问题描述
我想向评论该帖子的用户分配一项任务。但是当我尝试继续 post_detail.html 时发生错误,即
/home/ 处的操作错误
没有这样的列:home_post.assign_to_id
PS:我已经完成了 python manage.py makemigrations 和 python manage.py migrate
所以,这是代码片段。如果需要,我可以提供更多信息。提前致谢!
views.py
def SaveAssigned(request):
if request.method == "POST":
if request.POST.get('assigned'):
savevalue = Post()
savevalue.assign_to = request.POST.get('assigned')
savevalue.save()
return render(request,'post_detail.html')
else:
return render(request,'post_detail.html')
class PostDetailView(DetailView):
model = Post
# template_name = 'home/post_detail.html'
def get_context_data(self,*args,**kwargs):
post_available = get_object_or_404(Post,id=self.kwargs['pk'])
cat_menu = Category.objects.all()
context = super(PostDetailView,self).get_context_data()
context["cat_menu"] = cat_menu
return context
urls.py
urlpatterns = [
path('',PostListView.as_view(),name='home'),path('post/<int:pk>/',PostDetailView.as_view(),name='post-detail'),path('post/new/',PostCreateView.as_view(),name='post-create'),path('post/<int:pk>/update/',PostUpdateView.as_view(),name='post-update'),path('post/<int:pk>/delete/',PostDeleteView.as_view(),name='post-delete'),path('post/<int:pk>/comment/',AddCommentView.as_view(),name='add-comment'),path('add_category/',AddCategoryView.as_view(),name='add-category'),path('category/<str:cats>/',CategoryView,name='category'),path('category-list/',CategoryListView,name='category-list'),]
models.py
class Post(models.Model):
title = models.CharField(max_length = 100)
snippet = models.CharField(max_length= 200)
content = RichTextField(blank=True,null=True)
date_posted = models.DateTimeField(default = timezone.Now)
author = models.ForeignKey(User,on_delete= models.CASCADE)
category = models.CharField(max_length=255,default='Coding')
assign_to = models.ForeignKey(User,related_name='assign_to',on_delete=models.CASCADE,null=True)
def __str__(self):
return self.title + ' | ' + str(self.author)
def get_absolute_url(self):
return reverse('home')
post_detail.html
{% extends 'users/base.html' %}
{% block body %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ object.author.profile.image.url }}">
<div class="media-body">
<div class="article-Metadata">
<a class="mr-2" href="#">{{ object.author }}</a>
<small class="text-muted">{{ object.category }}</small>
<small class="text-muted" style="float: right;">{{ object.date_posted|date:"F d,Y" }}</small>
</div>
<h2 class="article-title">{{ object.title }}</h2>
<p class="article-content">{{ object.content|safe }}</p>
</div>
</article>
{% if object.author == user %}
<div>
<a class="btn btn-outline-secondary bt-sm mt-1 mb-1" href="{% url 'post-update' object.id %}">Update</a>
<a class="btn btn-outline-danger bt-sm mt-1 mb-1" href="{% url 'post-delete' object.id %}">Delete</a>
<a class="btn btn-outline-primary bt-sm mt-1 mb-1" style="float: right;" href="">Done</a>
</div>
<form method="POST">
{% csrf_token %}
<select name="assigned">
<option selected disabled="true"> Select User</option>
{% if not post.comments.all %}
<option>NA</option>
{% else %}
{% for comment in post.comments.all %}
<option>{{ comment.author }}</option>
{% endfor %}
{% endif %}
</select>
<input type="submit" value="Assign" name="">
</form>
{% endif %}
<br>
<hr>
<h2>Comment Section</h2>
<br>
{% if not post.comments.all %}
No Comments yet
<br>
<a href="{% url 'add-comment' post.pk %}">Be the first Ont to Comment!</a>
{% else %}
<a href="{% url 'add-comment' post.pk %}">Add a comment</a>
<br>
{% for comment in post.comments.all %}
<strong>
{{ comment.author }} - {{ comment.date_added }}
</strong>
<br>
{{ comment.body }}
<br><br>
{% endfor %}
<hr>
{% endif %}
<br><br>
{% endblock %}
解决方法
当您的模型不包含特定列并且您尝试使用特定列名称在该模型中添加值时,会发生此错误。
您可以通过以下步骤解决此问题 - 步骤 - 1. python manage.py makemigration 步骤 - 2. python manage.py migrate