【问题标题】:White HTML page after redirection with Django CBV model使用 Django CBV 模型重定向后的白色 HTML 页面
【发布时间】:2018-03-28 21:30:46
【问题描述】:

我遇到了一个问题,但我找不到解决此问题的方法。 一切似乎都很好,但它不起作用。

这是过程:

我有一个模板,其中包含有关刚刚由 Django 表单创建的对象的所有信息。 我有一个按钮必须重定向到另一个模板,考虑到object id,但是当我重定向到这个模板时,我得到一个 html 白页。

这是我的模型:

class Societe(models.Model):

    NumeroIdentification            = models.CharField(max_length=30, null=True, verbose_name='Numero Identification physique', unique=True)
    Nom                             = models.CharField(null= False, max_length=30, verbose_name='Nom de Société')
    Etat                            = models.CharField(max_length = 30, choices = CHOIX_ETAT_SOCIETE, null=False, verbose_name="Etat")
    ...

    def get_absolute_url(self):
        return reverse_lazy('SocieteResume', kwargs={'id': self.id})

    def __unicode__(self):
        return unicode (self.id, self.NumeroIdentification, self.Nom, ...)

我有一个头等舱,可以详细显示创建的对象:

class IdentitySocieteResumeView(LoginRequiredMixin, ListView) :

    template_name = 'Identity_Societe_Resume.html'
    model = Societe

    def get_context_data(self, **kwargs) :

        context_data = super(IdentitySocieteResumeView, self).get_context_data(**kwargs)

        id = self.kwargs['id']
        societe = get_object_or_404(Societe, pk=id)

        obj = Societe.objects.filter (Nom=societe.Nom, SIRET=societe.SIRET, SIREN=societe.SIREN, Ville=societe.Ville)

        if obj:
            sc_obj = obj[0] 

            ...

        return context_data

使用具有此按钮的关联模板以重定向到下一个模板:

<form method='POST' action="{% url 'SocietePDF' societe.id %}">{% csrf_token %}
{% csrf_token %}
<button>Générer le PDF de la Fiche d'Identification </button>
</form>

此按钮重定向到(我必须获得 id 才能获得个性化视图/模板):

class IdentitySocietePDFCreatingView(LoginRequiredMixin, TemplateView) :

    template_name = 'Identity_Societe_PDF.html'
    model = Societe

    def get_context_data(self, **kwargs) :

        context_data = super(IdentitySocietePDFCreatingView, self).get_context_data(**kwargs)

        id = self.kwargs['id']
        societe = get_object_or_404(Societe, pk=id)

        obj = Societe.objects.filter (Nom=societe.Nom, SIRET=societe.SIRET, SIREN=societe.SIREN, Ville=societe.Ville)

        ...

        return context_data

我的 urls.py 文件如下所示:

from django.conf.urls import url
from Identity.views import IdentityIndividuFormView, IdentityHomepageView, IdentityChoiceUpdateView, IdentitySocieteFormView, IdentitySocieteResumeView, IdentitySocietePDFCreatingView
from . import views

urlpatterns = [
    url(r'^Homepage$', IdentityHomepageView.as_view(), name="Home"),
    url(r'^Person/ChoiceUpdate/$', IdentityChoiceUpdateView.as_view(), name="IdentityChoice"),
    url(r'^Person/Form/$', IdentityIndividuFormView.as_view(), name="IndividuFormulaire"),
    url(r'^Company/Form/$', IdentitySocieteFormView.as_view(), name = "SocieteFormulaire"),
    url(r'^Person/Form/Resume/(?P<id>\d+)/$', views.IdentityIndividuResume, name="IndividuResume"),
    url(r'^Company/Form/Resume/(?P<id>\d+)/$', IdentitySocieteResumeView.as_view(), name="SocieteResume"),
    url(r'^Person/Update/$', views.IdentityIndividuUpdateAll, name="Edition"),
    url(r'^Company/Update/$', views.IdentitySocieteUpdateAll, name="EditionSociete"),
    url(r'^Person/Research/$', views.IdentityIndividuResearching, name="IndividuRecherche"),
    url(r'^Company/Research/$', views.IdentitySocieteResearching, name="SocieteRecherche"),
    url(r'^Company/Research/Fraud/$', views.IdentitySocieteFraudResearching, name="SocieteRechercheFraude"),
    url(r'^Company/Research/Employe/$', views.IdentitySocieteEmploye, name="SocieteRechercheEmploye"),
    url(r'^Person/Read/PDF/$', views.IdentityIndividuPDFReading, name="Consultation"),
    url(r'^Company/Read/PDF/$', views.IdentitySocietePDFReading, name="SocieteConsultation"),
    url(r'^Person/Delete/$', views.IdentityIndividuDelete, name="Suppression"),
    url(r'^Person/Form/PDF/(?P<id>\d+)/$', views.IdentityIndividuPDFCreating, name="IndividuPDF"),
    url(r'^Company/Form/PDF/(?P<id>\d+)/$', IdentitySocietePDFCreatingView.as_view(), name="SocietePDF"),
    url(r'^Statistics/$', views.IdentityStatistics, name="Statistiques"),
    url(r'^Person/Update/Civility/$', views.IdentityIndividuUpdateCivility, name="EditionCivilite"),
    url(r'^Person/Update/Coordonates/$', views.IdentityIndividuUpdateCoordonates, name="EditionCoordonnees"),
    url(r'^Person/Update/Contact/$', views.IdentityIndividuUpdateContact, name="EditionContact"),
]

奇怪的是:当我单击按钮以重定向到 SocietePDF 时,我得到一个白色的 HTML 页面,但如果我剪切/粘贴 url,我可以访问模板。

我的过程中缺少什么?

谢谢!

【问题讨论】:

  • En anglais pour "blanc" dans le sens de "vide" on dit bien "blank" et non pas "white"。
  • @DanielRoseman 好吧,对不起,我的 html 页面全是白色的,但空白更适合 ^^ 我不知道你会说法语;)
  • 我有很多才能...:) 无论如何,我会尝试的一件事是将表单操作从 POST 更改为 GET,因为您的 IdentitySocietePDFCreatingView 不需要 POST 请求。
  • @DanielRoseman 你又是对的一次!当我试图将 FBV 迁移到 CBV 时,我没有注意这种表单方法。谢谢 !也许您可以添加您的评论作为答案,我将验证您的答案作为解决方案。

标签: django django-templates django-views django-class-based-views


【解决方案1】:

您的 IdentitySocietePDFCreatingView 不期待 POST 请求;您应该将表单操作更改为 GET。

【讨论】:

    猜你喜欢
    • 2019-03-20
    • 2013-09-05
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2017-09-27
    • 1970-01-01
    • 2012-06-18
    相关资源
    最近更新 更多