打开文章时出现此错误
UserAccount匹配查询不存在
views.py
class ArticleDetailView(DetailView, CategoryAndArticleListMixin):
model = Article
template_name = 'post_detail.html'
def get_context_data(self, *args, **kwargs):
context = super(ArticleDetailView, self).get_context_data(*args, **kwargs)
context['article'] = self.get_object()
context['article_comments'] = self.get_object().comments.all()
context['form'] = CommentForm
context['current_user'] = UserAccount.objects.get(user=self.request.user) #probably where the error arises
return context
我的UserAccount模型
models.py
class UserAccount(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.DO_NOTHING)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField()
favorite_articles = models.ManyToManyField(Article)
def __str__(self):
return self.user.username
def get_absolute_url(self):
return reverse('account_user', kwargs={'user': self.user.username})
urls.py
urlpatterns = [
url(r'^$', CategoryListView.as_view(), name='base_view'),
url(r'category/(?P<slug>[-\w]+)/$', CategoryDetailView.as_view(), name='category-detail'),
url(r'account_user/(?P<user>[-\w]+)/$', UserAccountView.as_view(), name='account_user'),
url(r'(?P<category>[-\w]+)/(?P<slug>[-\w]+)/$', ArticleDetailView.as_view(), name='article-detail'),
url(r'show_article_image/$', DynamicArticleImageView.as_view(), name='article_image'),
url(r'add_comment/$', CreateCommentView.as_view(), name='add_comment'),
url(r'display_articles_by_category$', DisplayArticlesByCategoryView.as_view(), name='articles_by_category'),
url(r'user_reaction$', UserReactionView.as_view(), name='user_reaction'),
url(r'registration/$', RegistrationView.as_view(), name='registration'),
url(r'login/$', LoginView.as_view(), name='login'),]
我使用逻辑的html代码 post_detail.html
<p class="add_to_favorites {{ article.slug }}">
{% if article in current_user.favorite_articles.all %}
<button class="btn btn-disabled">Добавлено в избранное</button>
{% else %}
<a href=""><button class="btn btn-danger">Добавить в избранное</button></a>
{% endif %}
有人可以帮我解决这个问题吗?