0

我有一个ModelForm,我想在多个位置显示。例如,在列表视图中,文章列表下方。我可以通过将它放在ListView中的get_context_data()中来实现。我也想在自己的模板中显示表单。如何编写基于类的基于视图的表单显示和从其他模板提交?

我为表单创建了一个视图,但我不确定如何实际编写它。

我在我的模型中定义的get_absolute_url()

class Article(models.Model): 
    author = models.ForeignKey('auth.User') 
    title = models.CharField(max_length=200) 
    text = models.TextField() 
    categories = models.ManyToManyField(Category) 
    city = models.ForeignKey(City) 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse_lazy('article', args=self.id) 

的形式本身的看法是:

Views.py

class ArticleSubmitView(CreateView): 
    model = Article 
    form_class = ArticleSubmitForm 
    # is initial necessary? 
    inital = {'title': '', 'text': '', 'categories': '', 'city': ''} 
    # success url not necessary because model has get_absolute_url 
    # however it does not redirect to the article 
    template_name = 'articles/article-submit.html' 
    # handle post data from other template/view 
    # ??? 

模板包括形式(对于同一件事ListView模板)。

article-submit.html

{% extends 'articles/base.html' %} 
{% block article-submit %} 
    {% include 'articles/article-form.html' %} 
{% endblock article-submit %} 

的形式提交给调用CreateView的网址:

article-form.html

<form action="{% url 'article-submit' %}" method="POST"> 
    {% csrf_token %} 
    {% for field in form %} 
    <!--- etc. ---> 
    {% endfor %} 
</form> 

urls.py

from django.conf.urls import url 
from .views import ArticlesView, ArticleSubmitView 

urlpatterns = [ 
    url(r'^$', ArticlesView.as_view(), name='articles'), 
    # some urls left out for brevity 
    url(r'^article-submit/$', ArticleSubmitView.as_view(), name='article-submit'), 
] 

但是,表单不会从列表模板提交,也不会从表单模板本身提交。它也不会重定向,或显示任何错误消息。

我在做什么错?

Full code is available here

编辑:

检查,看看是否形式是否有效或不喜欢这表明我的形式实际上是无效的:

class ArticleSubmitView(CreateView): 
    model = Article 
    form_class = ArticleSubmitForm 
    # success url not necessary because model has get_absolute_url 
    # however it does not redirect to the article 
    template_name = 'articles/article-submit.html' 
    # handle post data from other template/view 
    # ??? 
    def form_valid(self, form): 
     print('form is valid') 
    def form_invalid(self, form): 
     print('form is invalid') 
     print(form.errors) 

但是我得到:
AttributeError at /article-submit/
'ArticleSubmitForm' object has no attribute 'errors'

将表单渲染为{{form}}时会出现同样的情况

+0

它看起来像你一次尝试做几件事 - 自定义模板渲染,而不是仅仅使用来自第三方应用的“{{form}}”,“MultiModelForm”,并从一个url提交到另一个。如果您尝试一次完成其中一项而不是一次完成其中一项,则调试将变得更加容易。如果您尝试在form_invalid方法中打印form.errors,它可能会显示出错。如果这没有帮助,尽可能简化您的视图和模板,并首先得到一个简单的例子。 – Alasdair

+0

@Alasdair是真的。我只是尝试打印form.errors,并编辑了OP来反映这一点。接下来尝试使用{{form}}。谢谢! – Flobin

回答

0

事实证明,我不需要django-betterforms。常规模型表现得很好。还有一些其他的错误。

这是代码。

models.py

class Article(models.Model): 
    author = models.ForeignKey('auth.User') 
    title = models.CharField(max_length=200) 
    text = models.TextField() 
    categories = models.ManyToManyField(Category) 
    city = models.ForeignKey(City) 

    def __str__(self): 
     return self.title 

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

views.py

class ArticleSubmitView(CreateView): 
    model = Article 
    form_class = ArticleForm 
    template_name = 'articles/article-submit.html' 

    def form_valid(self, form): 
     print('form is valid') 
     print(form.data) 
     obj = form.save(commit=False) 
     obj.author = self.request.user 
     obj.save() 
     return HttpResponseRedirect(reverse('article', kwargs={'pk': obj.id})) 

的URL和模板保持(在很大程度上)如上述。