4

我购买并正在阅读这本书 Django 的两个勺子:Django 1.5 的最佳实践,其中有一个基于类的视图示例。在此实施之后,我在提交表单后收到错误。

ImproperlyConfigured at /NonProfitCreate/
No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model

做研究我遇到了这个问题Django - 基于类的通用视图 - “没有要重定向到的 URL”

我希望 get_absolute_url 在我的程序中工作

这是我的forms.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  npp/forms.py

from django import forms

from .models import NonProfit

class NonProfitCreateForm(forms.ModelForm):
    class Meta:
        model = NonProfit
        fields = ("name","contact_name","email","phone","address","image","tags",)

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        return super(NonProfitCreateForm, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        kwargs['commit']=False
        obj = super(NonProfitCreateForm, self).save(*args, **kwargs)
        if self.request:
            obj.user = self.request.user
            obj.save()

class NonProfitUpdateForm(NonProfitCreateForm):
    class Meta:
        model = NonProfit

这是我的 models.py 和视图文件

from django.db import models
from django.contrib.auth.models import User
from django.db.models import permalink
from django_extensions.db.fields import AutoSlugField
from django.contrib import admin

from django.core.urlresolvers import reverse

import tagging
from tagging.models import Tag
# Create your models here.
''' this is for the Non-Profit Proccess '''

class NonProfit (models.Model):
    User = models.ForeignKey(User)
    name = models.CharField(max_length=100)
    contact_name = models.CharField(max_length=100)
    email = models.EmailField(max_length=75)
    phone = models.CharField(max_length=20)
    address = models.TextField(max_length=3000)
    image = models.ImageField(upload_to='photos/%Y/%m/%d',blank=True)


    slug = models.SlugField(max_length=128)
    slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))
    tags = tagging.fields.TagField()

    def get_absolute_url(self):
        return reverse("npp/nonprofit_detail", kwargs={"slug": self.slug})  

    def __unicode__(self):
        return self.name

    def get_tags(self):
        return Tag.objects.get_for_object(self)




  # Create your views here.
    # Auction/npp/views.py

from Auction.views import ActionMixin

from django.contrib import messages
from django.views.generic import CreateView, UpdateView, DetailView

from braces.views import LoginRequiredMixin

from forms import NonProfitCreateForm,NonProfitUpdateForm


from models import NonProfit

class NonProfitCreateView(LoginRequiredMixin,ActionMixin,CreateView):
    model = NonProfit
    action = "created"
    form_class = NonProfitCreateForm

class NonProfitUpdateView(LoginRequiredMixin,ActionMixin,UpdateView):
    model = NonProfit
    action = "updated"
    form_class = NonProfitUpdateForm


class NonProfitDetailView(DetailView):
    model = NonProfit


# Auction/views.py
class ActionMixin(object):
    @property
    def action(self):
        msg = "{0} is missing action.".format(self.__class__)
        raise NotImplementedError(msg)

    def form_valid(self, form):
        msg = "{0}!".format(self.action)
        messages.info(self.request, msg)
        return super(ActionMixin, self).form_valid(form)

网址.py

url(
    regex=r'^NonProfitCreate/',
    view=NonProfitCreateView.as_view(),
    name='NonProfitCreate',
),
url(
    regex=r'^NonProfit/(?P<slug>[-\w\d]+)/',
    view=NonProfitDetailView.as_view(),
    name='NonProfit'
    )

这是我的堆栈跟踪,django 大括号突出显示,并且

/home/talisman/projects/Auction/Auction/views.py in form_valid
        return super(ActionMixin, self).form_valid(form) 

EEnvironment:


Request Method: POST
Request URL: http://127.0.0.1:8000/NonProfitCreate/

Django Version: 1.5.1
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'django.contrib.comments',
 'django.contrib.sitemaps',
 'zinnia',
 'tagging',
 'mptt',
 'south',
 'misc',
 'adm',
 'registration',
 'npp',
 'blogs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/django_braces-1.0.0-py2.7.egg/braces/views.py" in dispatch
  98.             **kwargs)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/base.py" in dispatch
  86.         return handler(request, *args, **kwargs)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/edit.py" in post
  199.         return super(BaseCreateView, self).post(request, *args, **kwargs)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/edit.py" in post
  165.             return self.form_valid(form)
File "/home/talisman/projects/auction/Auction/views.py" in form_valid
  54.       return super(ActionMixin, self).form_valid(form)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/edit.py" in form_valid
  128.         return super(ModelFormMixin, self).form_valid(form)
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/edit.py" in form_valid
  65.         return HttpResponseRedirect(self.get_success_url())
File "/home/talisman/virt_env/Auction/local/lib/python2.7/site-packages/Django-1.5.1-py2.7.egg/django/views/generic/edit.py" in get_success_url
  119.                     "No URL to redirect to.  Either provide a url or define"

Exception Type: ImproperlyConfigured at /NonProfitCreate/
Exception Value: No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.
4

5 回答 5

12

产生此异常是因为self.object = None在有效编辑后尝试重定向时。由于 的值self.objectform.save()调用的结果,因此该错误最可能的原因是您已覆盖 中的save()方法NonProfitCreateForm,但忘记返回保存的对象。

Form.save()方法应返回已保存且不应保存的对象None

NonProfitCreateForm可以修改如下所示:

class NonProfitCreateForm(forms.ModelForm):
    ...
    def save(self, *args, **kwargs):
        kwargs['commit']=False
        obj = super(NonProfitCreateForm, self).save(*args, **kwargs)
        if self.request:
            obj.user = self.request.user
            obj.save()
        return obj #<--- Return saved object to caller.

您的方法的前两行save()将从输入的表单数据创建一个模型实例。但是因为commit=False,对象不会被保存到数据库中。如果self.request您的表单实例上不存在,则返回的对象将没有数据库主键,并且 get_absolute_url仍然会失败。

因此,您要确保request在实例化时始终将参数传递给您的表单。默认情况下不会发生这种情况,因此您需要安排视图代码以使用request参数实例化表单。

查看代码,FormMixin您可以看到有一个get_form_kwargs函数可以确定要传递给任何实例化表单的参数。您需要传递request=self.request,因此在您的视图中覆盖get_form_kwargs以添加所需的参数,如下所示:

class NonProfitCreateView(LoginRequiredMixin,ActionMixin,CreateView):
    model = NonProfit
    action = "created"
    form_class = NonProfitCreateForm

    def get_form_kwargs(self):
        # Ensure the current `request` is provided to NonProfitCreateForm.
        kwargs = super(NonProfitCreateView, self).get_form_kwargs()
        kwargs.update({ 'request': self.request })
        return kwargs

CreateView使用修改后的get_form_kwargs函数创建一个子类并NonProfitCreateView从子类派生可能是一个更好的主意。

于 2013-05-20T14:32:52.230 回答
3

当您使用 时reverse,请使用您希望反转的 url 模式的名称。

您希望重定向到此网址:

url(
    regex=r'^NonProfit/(?P<slug>[-\w\d]+)/',
    view=NonProfitDetailView.as_view(),
    name='NonProfit'
    )

因此,您的get_absolute_url方法应该是:

def get_absolute_url(self):
    return reverse("NonProfit", kwargs={"slug": self.slug})  
于 2013-05-19T23:52:39.173 回答
1

您可以覆盖基于类的视图的get_success_url功能。像这样:

def get_success_url(self):
    return reverse("NonProfit", kwargs={"slug": self.object.slug})  
于 2013-05-20T12:08:13.937 回答
1

尝试从您的方法中删除@permalink装饰器。get_absolute_url它不能与reverse.

此外,Django 文档指出以下内容:

permalink不再推荐使用装饰器。您应该reverse()在方法的主体中使用get_absolute_url

于 2013-04-14T22:15:32.083 回答
0

阅读Austin Phillips的第一句话后解决了这个问题:

    def form_valid(self, form):
        article = form.save(commit=False)
        article.author = self.request.user
        self.object = article.save()
        return super().form_valid(form)

关键是 savearticle.save()的结果回到self.object

于 2019-03-29T02:08:13.700 回答