gpt4 book ai didi

python - DeleteView 有 2 个争论帖子和用户

转载 作者:行者123 更新时间:2023-12-01 09:31:04 25 4
gpt4 key购买 nike

我有一个包含 2 个条件“帖子”和“用户”的删除 View 。

用户需求由 self.object.user = self.request.user 满足,帖子需求由 slug = self.kwargs['slug'] 满足(我认为这可能是罪魁祸首)

我的观点正确吗?

我是python新手,请原谅任何愚蠢的错误。

View .py

class ProofDelete(LoginRequiredMixin, DeleteView):
model = Proof

def delete(self, *args, **kwargs):
return super().delete(*args, **kwargs)

def get_success_url(self, *args, **kwargs):
slug = self.kwargs['slug']
print(slug)
obj = get_object_or_404(Post, slug=slug)
url_ = obj.get_absolute_url()
user = self.request.user
if user.is_authenticated():
if user in obj.made.all():
obj.made.remove(user)
else:
obj.made.add(user)
return url_

模型.py

User = get_user_model() 
class Proof(models.Model):
user = models.ForeignKey(User, related_name='proofmade')
post = models.ForeignKey(Post, related_name='proofmade')
made_at = models.DateTimeField(auto_now=True)
image_of_proof= models.ImageField()
proof_ = models.ImageField()
suggestions = models.TextField(max_length=1000)

def __str__(self):
return self.post.title

url.py

    app_name = 'proof'

urlpatterns = [

url(r'^new_proof/(?P<slug>[-\w]+)/$', views.ProofCreate.as_view(), name='new_proof'),

url(r'^proof_delete/(?P<pk>\d+)/$', views.ProofDelete.as_view(),name='proof_delete'),

也尝试过

url (r'^proof_delete/(?P<slug>[-\w]+)/(?P<pk>\d+)/$', views.ProofDelete.as_view(), name='proof_delete'),

我收到以下错误。指示 View 中的错误

Error Message Click to see Error message

Same error message after scrolling down. Click to open

最佳答案

答案就在模板中。在“if”语句后添加一个for循环解决了这个问题。这样做可以处理 Url 的两个参数。 “post”模型的“slug”和“proof”模型的“pk”。如果没有 forloop,几乎不可能同时满足 URL 的两个参数

正确的网址感谢“Paulo Almeida”

url (r'^proof_delete/(?P<slug>[-\w]+)/(?P<pk>\d+)/$', views.ProofDelete.as_view(), name='proof_delete') 

下面是模板

{% if user in post.made.all %}
{% for proof in user.proofmade.all %}
<a href="{% url 'proof:proof_delete' slug=post.slug pk=proof.pk %}">
<img src="{% static 'images/thumbs_up_RED.png' %}" height="25px">
</a><br/>
{% endfor %}
{% else %}
<a href="{% url 'proof:new_proof' slug=post.slug %}">
<img src="{% static 'images/thumbs_up_BLANK.png' %}" height="25px">
</a><br/>
{% endif %}

关于python - DeleteView 有 2 个争论帖子和用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49960589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com