【发布时间】: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