我正在创建一个投诉管理系统,用户可以输入他们的投诉,以及查看和编辑他们,也可以查看其他用户的投诉。在用户可以查看其他投诉的页面上,没有显示任何投诉或数据。我做什么好?
这应该会显示出来:

但这就是我们所看到的:

我的models.py:
class Profile(models.Model):
user = models.OneToOneField(User,null= True , blank = True, on_delete= models.CASCADE)
profile_pic = models.ImageField(default = "msi.jpg", null = True, blank= True, upload_to=
'static/profileimages')
first = models.CharField(max_length=500, null=True)
last = models.CharField(max_length=500, null=True)
email = models.CharField(max_length=500, null=True)
mobile_number = models.IntegerField(null=True)
location = models.CharField(max_length= 500, null= True)
postal = models.IntegerField(null=True)
def __str__(self):
return self.first
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.reportnumberviews.py:
class OtherPeoplesComplaints(TemplateView):
model = Complaint
form_class = ComplaintForm
template_name = 'userComplaints.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["complaints"] = self.model.objects.exclude(user = self.request.user)forms.py:
class CreateUserForm(UserCreationForm):
email = forms.EmailField()
password2 = None
class Meta:
model = User
fields = ['username','first_name', 'last_name','email', 'password1']
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = '__all__'
exclude = ['user']
widgets = {
'profile_pic': forms.FileInput()
}
class DateInput(forms.DateInput):
input_type = 'date'
class ComplaintForm(ModelForm):
class Meta:
model = Complaint
fields = '__all__'
widgets = {
'reportnumber': forms.TextInput(attrs={'placeholder': 'Report number'}),
'event_type': forms.TextInput(attrs={'placeholder': 'Event type'}),
'eventdate': DateInput(),
'device_problem': forms.TextInput(attrs={'placeholder': 'Device Problem'}),
'event_text': forms.Textarea(attrs={'style': 'height: 130px;width:760px'}),
'manufacturer': forms.TextInput(attrs={'placeholder': 'Enter Manufacturer Name'}),
'product_code': forms.TextInput(attrs={'placeholder': 'Enter Product Code'}),
'brand_name': forms.TextInput(attrs={'placeholder': 'Enter Brand Name'}),
'exemption': forms.TextInput(attrs={'placeholder': 'Enter Exemption'}),
'patient_problem': forms.TextInput(attrs={'placeholder': 'Enter Patient Problem'}),
}
def clean(self):
cleaned_data = super(ComplaintForm, self).clean()
reportnumber = cleaned_data.get('reportnumber')
event_text = cleaned_data.get('event_text')
if not reportnumber and not event_text:
raise forms.ValidationError('You have to write something!')
return cleaned_data模板:
<div class="col-lg middle middle-complaint-con">
<i class="fas fa-folder-open fa-4x comp-folder-icon"></i>
<h1 class="all-comp">All 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 complaints %}
<a href="" style="color:black;">
<div class="container comp-con-{{forloop.counter0}}">
{% if request.user.profile.profile_pic.url %}
<img src={{request.user.profile.profile_pic.url}} class="comp-con-img" alt=""> {% else %}
<img src="{% static 'profileimages/msi.jpg' %}" class="comp-con-img" alt=""> {% endif %}
<p class="comp-level-1">{{c.first_name}}</p>
<p class="comp-title-1">{{c.event_type}}</p>
<p class="comp-sub-1">{{c.event_text}}</p>
</div>
</a> {%endfor%}
</div>发布于 2021-07-22 14:26:57
在获取上下文数据方法的views.py中...它实际上没有返回上下文。例如,您拥有:
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["complaints"] = self.model.objects.exclude(user = self.request.user)您应该在此处添加一个return语句,以返回您已更新的上下文。
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context["complaints"] = self.model.objects.exclude(user = self.request.user)
return contexthttps://stackoverflow.com/questions/68479905
复制相似问题