我在 Django 中的代码似乎有什么问题?
What seems to be wrong with my code in django?
urls.py:
urlpatterns = [
....
path('My-History/', accounts.views.History, name='MyHistory'),
path('Complaint/<int:id>/edit', accounts.views.EditComplaint.as_view(), name='Complaint')
]
views.py:
def History(request):
complaint_data = Complaint.objects.filter(user=request.user)
context = { 'complaint':complaint_data }
return render(request, 'myHistory.html', context)
class EditComplaint(UserPassesTestMixin, UpdateView):
model = Complaint
fields = ('info', 'reportnumber', 'eventdate', 'event_type', 'device_problem',
'manufacturer', 'product_code', 'brand_name', 'exemption', 'patient_problem', 'event_text',
'document')
def form_valid(self, request):
complaint = request.user.complaint
form = ComplaintForm(instance=complaint)
if request.method == 'POST':
form = ComplaintForm(request.POST, request.FILES, instance=complaint)
if form.is_valid():
form.save()
context = {'form': form}
return render(request, 'newcomplaint.html', context)
def test_func(self):
complain = self.get_object()
if self.request.user == complain.user:
return True
raise Http404(_('This complain does not exist'))
models.py:
class Complaint(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE, null = True, blank=True)
id = models.AutoField(blank=False, primary_key=True)
reportnumber = models.CharField(max_length=500 ,null = True, blank= False)
eventdate = models.DateField(null=True, blank=False)
event_type = models.CharField(max_length=300, null=True, blank=True)
device_problem = models.CharField(max_length=300, null=True, blank=True)
manufacturer = models.CharField(max_length=300, null=True, blank=True)
product_code = models.CharField(max_length=300, null=True, blank=True)
brand_name = models.CharField(max_length = 300, null=True, blank=True)
exemption = models.CharField(max_length=300, null=True, blank=True)
patient_problem = models.CharField(max_length=500, null=True, blank=True)
event_text = models.TextField(null=True, blank= True)
document = models.FileField(upload_to='static/documents', blank=True, null=True)
def __str__(self):
return self.reportnumber
我的历史模板:
<div class="col-lg middle middle-complaint-con">
<i class="fas fa-folder-open fa-4x comp-folder-icon"></i>
<h1 class="all-comp">My Complaints</h1>
<p class="all-comp-txt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
{%for c in complaint %}
<a href="{% url 'Complaint' complaint.id %}" style="color:black;">
<div class="container comp-con-{{forloop.counter0}}">
<p style="color: #D37A19; margin-left: -130px; margin-top: -5px;">Report number:</p>
<p class="history-level-1">{{c.reportnumber}}</p>
<p class="comp-title-1">{{c.event_type}}</p>
<p class="comp-sub-1">{{c.event_text}}</p>
</div>
</a> {%endfor%}
</div>
注意:当我删除“{% url 'Complaint' complaint.id %}”时,我的历史页面出现了,但我无法进入详细信息页面个人投诉。如果我将此 url 保留在其中,则会显示此页面:
我做错了什么??
你的url“投诉”被设置为接收一个参数......但是正如错误回溯所说,没有任何参数被传递到投诉url......
改变这个并尝试,
这个
{%for c in complaint %}
<a href="{% url 'Complaint' complaint.id %}" style="color:black;">
进入
{%for c in complaint %}
<a href="{% url 'Complaint' c.id %}" style="color:black;">
由于您正在使用 'c' 遍历投诉查询对象,因此您必须通过 c.id
访问每个 ID
urls.py:
urlpatterns = [
....
path('My-History/', accounts.views.History, name='MyHistory'),
path('Complaint/<int:id>/edit', accounts.views.EditComplaint.as_view(), name='Complaint')
]
views.py:
def History(request):
complaint_data = Complaint.objects.filter(user=request.user)
context = { 'complaint':complaint_data }
return render(request, 'myHistory.html', context)
class EditComplaint(UserPassesTestMixin, UpdateView):
model = Complaint
fields = ('info', 'reportnumber', 'eventdate', 'event_type', 'device_problem',
'manufacturer', 'product_code', 'brand_name', 'exemption', 'patient_problem', 'event_text',
'document')
def form_valid(self, request):
complaint = request.user.complaint
form = ComplaintForm(instance=complaint)
if request.method == 'POST':
form = ComplaintForm(request.POST, request.FILES, instance=complaint)
if form.is_valid():
form.save()
context = {'form': form}
return render(request, 'newcomplaint.html', context)
def test_func(self):
complain = self.get_object()
if self.request.user == complain.user:
return True
raise Http404(_('This complain does not exist'))
models.py:
class Complaint(models.Model):
user = models.ForeignKey(User, on_delete= models.CASCADE, null = True, blank=True)
id = models.AutoField(blank=False, primary_key=True)
reportnumber = models.CharField(max_length=500 ,null = True, blank= False)
eventdate = models.DateField(null=True, blank=False)
event_type = models.CharField(max_length=300, null=True, blank=True)
device_problem = models.CharField(max_length=300, null=True, blank=True)
manufacturer = models.CharField(max_length=300, null=True, blank=True)
product_code = models.CharField(max_length=300, null=True, blank=True)
brand_name = models.CharField(max_length = 300, null=True, blank=True)
exemption = models.CharField(max_length=300, null=True, blank=True)
patient_problem = models.CharField(max_length=500, null=True, blank=True)
event_text = models.TextField(null=True, blank= True)
document = models.FileField(upload_to='static/documents', blank=True, null=True)
def __str__(self):
return self.reportnumber
我的历史模板:
<div class="col-lg middle middle-complaint-con">
<i class="fas fa-folder-open fa-4x comp-folder-icon"></i>
<h1 class="all-comp">My Complaints</h1>
<p class="all-comp-txt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
{%for c in complaint %}
<a href="{% url 'Complaint' complaint.id %}" style="color:black;">
<div class="container comp-con-{{forloop.counter0}}">
<p style="color: #D37A19; margin-left: -130px; margin-top: -5px;">Report number:</p>
<p class="history-level-1">{{c.reportnumber}}</p>
<p class="comp-title-1">{{c.event_type}}</p>
<p class="comp-sub-1">{{c.event_text}}</p>
</div>
</a> {%endfor%}
</div>
注意:当我删除“{% url 'Complaint' complaint.id %}”时,我的历史页面出现了,但我无法进入详细信息页面个人投诉。如果我将此 url 保留在其中,则会显示此页面:
我做错了什么??
你的url“投诉”被设置为接收一个参数......但是正如错误回溯所说,没有任何参数被传递到投诉url......
改变这个并尝试,
这个
{%for c in complaint %}
<a href="{% url 'Complaint' complaint.id %}" style="color:black;">
进入
{%for c in complaint %}
<a href="{% url 'Complaint' c.id %}" style="color:black;">
由于您正在使用 'c' 遍历投诉查询对象,因此您必须通过 c.id
访问每个 ID