首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查看投诉页面上的投诉未显示

查看投诉页面上的投诉未显示
EN

Stack Overflow用户
提问于 2021-07-22 14:05:06
回答 1查看 26关注 0票数 0

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

这应该会显示出来:

但这就是我们所看到的:

我的models.py:

代码语言:javascript
复制
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.reportnumber

views.py:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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

模板:

代码语言:javascript
复制
 <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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-22 14:26:57

在获取上下文数据方法的views.py中...它实际上没有返回上下文。例如,您拥有:

代码语言:javascript
复制
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语句,以返回您已更新的上下文。

代码语言:javascript
复制
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 context
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68479905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档