【问题标题】:How do I open my edit complaint page for one specific complaint in django如何在 django 中为一项特定投诉打开我的编辑投诉页面
【发布时间】:2021-07-19 11:57:21
【问题描述】:

我有一个查看投诉页面,用户可以在其中查看他/她提交的投诉。当用户点击其中一张卡片时,我需要打开一个新页面,用户可以在其中查看该投诉的详细信息并进行编辑。

它应该从这里开始:

到这里:他们可以查看详细信息并进行更改:

这是我的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

views.py:

def EditComplaints(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)

模板(查看历史页面):

<div class="col right-pro-con">
        <div class="img-cir">
            <form method='POST' action="" enctype="multipart/form-data">
                {% csrf_token %} {% if request.user.profile.profile_pic.url %}
                <img src={{request.user.profile.profile_pic.url}} alt="" width="100px" height="100px" class="pro-img"> {% else %}
                <img src="{% static 'profileimages/msi.jpg' %}" alt="" width="100px" height="100px" class="pro-img"> {% endif %}
                <p class="my-name">{{request.user.profile.first}}
                    <p>
                        <p class="my-email-id">{{request.user.profile.email}}</p>
            </form>
        </div>
        <a href="#" class="con-us">CONTACT US</a>
    </div>

模板(编辑投诉页面):

<div class="col-lg middle middle-complaint-con">
        <i class="fas fa-folder-open fa-4x comp-folder-icon"></i>
        <h1 class="all-comp">New Complaint</h1>

        <form class="" action="" method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <p class="sub-typ-wr">Submit Type</p>
            <a href="/Login/Add-Complaint/Document-Style/"><button type="button" class="btn btn-secondary document-btn">Document</button></a>

            <div class="rep-num">
                <label class="written-label" for="">Report Number</label>
                <div class="written-txt-field">{{form.reportnumber}}</div>
            </div>

            <div class="eve-dte">
                <label class="written-label" for="">Event Date</label>
                <div class="written-txt-field">{{form.eventdate}}</div>
            </div>

            <div class="eve-typ">
                <label class="written-label" for="">Event Type</label>
                <div class="written-txt-field">{{form.event_type}}</div>
            </div>

            <div class="dev-pro">
                <label class="written-label" for="">Device Problem</label>
                <div class="written-txt-field">{{form.device_problem}}</div>
            </div>

            <label class="written-label eve-txt" for="">Event Text</label>

            <div class="Manufacturer">
                <label class="written-label" for="">Manufacturer</label>
                <div class="written-txt-field">{{form.manufacturer}}</div>
            </div>

            <div class="pro-code">
                <label class="written-label" for="">Product Code</label>
                <div class="written-txt-field">{{form.product_code}}</div>
            </div>

            <div class="brand-name">
                <label class="written-label" for="">Brand Name</label>
                <div class="written-txt-field">{{form.brand_name}}</div>
            </div>

            <div class="exem">
                <label class="written-label" for="">Exemption</label>
                <div class="written-txt-field">{{form.exemption}}</div>
            </div>

            <div class="pat-pro">
                <label class="written-label" for="">Patient Problem</label>
                <div class="written-txt-field">{{form.patient_problem}}</div>
            </div>



            <div class="comp-textarea">{{form.event_text}}</div>
            <button type="button" class="btn btn-secondary attach-btn-1"><div class="fas fa-file-upload">{{form.document}}</div></button>
            <button type="submit" name="submit" class="btn btn-secondary save-btn-1"><i class="fas fa-save"></i> Save</button>
        </form>

    </div>

网址:

urlpatterns = [
   path('admin/', admin.site.urls),
   path('Home/', landing_page.views1.landing, name= 'Home'),
   path('Registration/', accounts.views.RegisterPage),
   path('Login/', accounts.views.LoginPage, name='Login'),
   path('Login/Profile/', accounts.views.profile, name='Profile'),
   path('Logout/', accounts.views.LogoutUser, name='Logout'),
   path('Login/Add-Complaint/', accounts.views.NewComplaint, name = 'New'),
   path('Login/Add-Complaint/Document-Style/', accounts.views.DocComplaint, name='doc'),
   path('My-History/', accounts.views.History, name='MyHistory'),
   path('Complaint/', accounts.views.EditComplaints, name='Complaint')
 ]

我该怎么做?我应该在代码中添加什么代码以打开特定的投诉详细信息并编辑该投诉页面?

【问题讨论】:

  • 你问我应该在列表页面的哪里添加一个链接让用户去编辑页面?
  • 不不...当用户想要打开特定投诉的编辑页面时,我不知道如何或使用什么类型的 url。我不知道如何从查看页面导航到该特定投诉的编辑页面。为此,我需要在我的代码中进行哪些更改。我有一份投诉清单,如果用户点击一个投诉,那么应该打开该投诉的编辑页面。那我该怎么做呢?

标签: django django-models django-views django-forms django-templates


【解决方案1】:

好的,所以你已经有了一个视图。您需要某种唯一标识符来帮助您确定用户想要编辑的实际对象。

因此,在您的 urls.py 中,您必须添加类似于以下内容的模式:

urlpatterns = [
    ...
    path('complain/<int:pk>/edit/', views.EditComplaint.as_view(), name='edit-complain'),
    ...
]

在您的views.py 中,以类似于以下方式处理它:

from django.views.generics import UpdateView
from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import Http404
from django.utils.translation import gettext_lazy as _

from .models import Complaint

class EditComplaint(UserPassesTestMixin, UpdateView):
    model = Complaint
    fields = ('info', )  # define whatever field that you want to render

    def form_valid(self, form):
        # do your form validation here

    def test_func(self):
        """ensuring the reporter themselves is updating the complain"""
        complain = self.get_object()

        if self.request.user == complain.user:
            return True
        raise Http404(_('This complain does not exist'))
  
    def success_url(self):
        # this is only required if your model doesn't have a `get_absolute_url` method
        # return the actual url of the instance

您现在只需在您的模板中添加一个链接,用于EditComplaint 视图(假设您的列表模板中已经有捐赠列表作为捐赠)。

类似的东西应该可以完成这项工作

{% for complaint in complaints %}
    <a href="{% url 'edit-complaint' complaint.pk %}">Edit Complaint</a>
{% endfor %}

【讨论】:

  • 有没有办法使用函数而不是类来做到这一点?使用类时,我的 url 路径出现错误
  • 您遇到了什么错误?在这种特殊情况下,您将不得不手动做很多事情,而基于 generic 类的视图会为您处理样板文件。
  • 我已将urlpattern 更新为使用as_view 方法。检查您的问题是否得到解决。
猜你喜欢
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多