【问题标题】:NoReverseMatch at /notes/create/ Reverse for 'notes_list' with no arguments not found. 1 pattern(s) tried: ['notes/list/(?P<username>[^/]+)$']NoReverseMatch at /notes/create/ Reverse for 'notes_list' 未找到任何参数。尝试了 1 种模式:['notes/list/(?P<username>[^/]+)$']
【发布时间】:2021-01-13 07:01:57
【问题描述】:

我不知道如何在侧边栏中添加用户笔记列表的链接。

错误出现在给定的以下模板行中

pages/base_stuff/side_bar.html。我想在给定的模板中添加链接,以便我可以将用户重定向到 usernotes_list 模板

 <li class="active treeview">
              <a href="{%url 'notes:notes_list' notes.author.username %}">
                <i class="fa fa-dashboard"></i> <span style='font-weight: normal;'>Your Notes</span> 
              </a>
 </li>

notes/urls.py

app_name = 'notes'

urlpatterns = [
  
    path('list/<str:username>',views.NotesListView.as_view(),name='notes_list'),

]

notes/views.py

class NotesListView(LoginRequiredMixin,ListView):
    login_url = '/accounts/login/'
    model = Notes
    context_object_name = 'notes_data'
    # paginate_by = 
    def get_queryset(self):
        user = get_object_or_404(auth.models.User, username=self.kwargs.get('username'))
        return Notes.objects.filter(author=user).order_by('-create_date')

notes/models.py

class Notes(models.Model):

    author = models.ForeignKey(auth.models.User, on_delete=models.CASCADE)
    title = models.CharField(max_length=150)
    essay = models.TextField()
    create_date = models.DateTimeField(default=timezone.now)


    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("notes:notes_detail", kwargs={"pk": self.pk})

pages/base_stuff/side_bar.html

{% load static %}
  <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
          <!-- Sidebar user panel -->
          <div class="user-panel">
            <div class="pull-left image">
              <img src="{{ user.userprofile.avatar.url }}" class="img-circle" alt="User Image" />
            </div>
            <div class="pull-left info">
              <p>{{ user.username }}</p>

              <a href="#">{% if user.is_active %}<i class="fa fa-circle text-success"></i> Online {% else %}<i class="fa fa-circle text-danger"></i> Offline {% endif %}</a>
            </div>
          </div>
          <!-- search form -->
          <!-- /.search form -->
          <!-- sidebar menu: : style can be found in sidebar.less -->
          <ul class="sidebar-menu">
            <li class="header">MAIN NAVIGATION</li>
            <li class="active treeview">
              <a href="{% url 'test' %}">
                <i class="fa fa-dashboard"></i> <span style='font-weight: normal;'>Home</span> 
              </a>
            </li>
            <li class="treeview">
              <a href="#">
                <i class="fa fa-files-o"></i>
               <span style='font-weight: normal;'>Courses</span>
               </a>
            </li>
            <li>
              <a href="{% url 'videos:videos_playlist' %}">
                <i class="fa fa-th"></i> <span style='font-weight: normal;'>Videos</span> 
              </a>
            </li>

            <li class="active treeview">
              <a href="{%url 'notes:notes_create'%}">
                <i class="fa fa-dashboard"></i> <span style='font-weight: normal;'>Create Note</span> 
              </a>
            </li>
             <li class="active treeview">
              <a href="{%url 'notes:notes_list' %}">
                <i class="fa fa-dashboard"></i> <span style='font-weight: normal;'>Your Notes</span> 
              </a>
            </li>
            <li class="treeview">
              <a href="{%url 'quiz:home'%}">
                <i class="fa fa-laptop"></i>
                <span style='font-weight: normal;'>Quiz</span>
              </a>  
            </li>
          {% if user.is_superuser %}
            <li class="treeview">
              <a href="{% url 'admin:index' %}">
                <i class="fa fa-edit"></i> <span style='font-weight: normal;'>Admin</span>
              </a>
            </li>
            {% endif %}

            <li class="treeview">
              <a href="{% url 'about' %}">
                <i class="fa fa-table"></i> <span style='font-weight: normal;'>About</span>
              </a>
            </li>
            <li>
              <a href="pages/calendar.html">
                <i class="fa fa-calendar"></i> <span style='font-weight: normal;'>Contact</span>
              </a>
            </li> 
        </section>

【问题讨论】:

  • 什么是notes.author.username 可以分享您的模型和完整模板吗?
  • 我可以分享模型,但模板很难,因为它很长,但如果真的有用,我仍然会分享
  • 模板必不可少...
  • 我补充说你可以在我的问题中看到
  • @FlashMaddy 我想你想从侧边栏显示当前登录用户的笔记列表?我说的对吗?

标签: python django django-models django-views django-templates


【解决方案1】:

您尚未在使用该链接的行中传递参数。这样做:

<li class="active treeview">
    <a href="{% url 'notes:notes_list' user.username %}">
        <i class="fa fa-dashboard"></i> <span style='font-weight: normal;'>Your Notes</span> 
    </a>
</li>

【讨论】:

  • 我问了一个新问题,你能回答吗link
【解决方案2】:

正如您在 cmets 中所说,如果您想在模板中显示当前用户的笔记列表,那么您可以尝试这样。

class NotesListView(LoginRequiredMixin,ListView):
    login_url = '/accounts/login/'
    model = Notes
    context_object_name = 'notes_data'
    # paginate_by = 
    def get_queryset(self):
        return Notes.objects.filter(author=self.request.user).order_by('-create_date')

现在你不需要从你的 url 传递额外的用户名参数

path('list/user/notes/',views.NotesListView.as_view(),name='notes_list'),

以及模板中的url

   <a href="{% url 'notes:notes_list' %}">

【讨论】:

  • 很好,这是一个非常好的方法。我确实找到了一些有用的方法来做这件事,你刚刚分享了谢谢@arjun。
  • btw @arjun 你能告诉我如何计算特定用户的列表项吗?
  • 我正在尝试以下几行,它给了我笔记模型中的对象总数。我只想统计登录的人如何在主页上看到他的笔记数。 def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["user_count"] = User.objects.count()
  • 试试这个。 {{request.user.notes_set.all.count}}
  • @FlashMaddy 这里 User 模型没有对 Notes 模型的显式引用,因此 django 会自动添加反向引用,默认情况下为 modelname_set。您可以通过在模型上使用 related_name 来覆盖它。请参阅docs 了解更多详情
猜你喜欢
  • 2019-04-17
  • 2019-11-01
  • 1970-01-01
  • 2020-07-20
  • 2018-11-25
  • 2020-10-14
  • 1970-01-01
  • 2022-07-04
  • 1970-01-01
相关资源
最近更新 更多