Django - 基于类的通用视图 - “没有要重定向到的 URL”

发布于 2024-11-14 07:05:12 字数 646 浏览 1 评论 0原文

我正在使用通用的 CreateView,例如:

#urls.py

from django.conf.urls.defaults import *
from django.views.generic import CreateView
from content.models import myModel

urlpatterns = patterns('myApp.views',
    (r'myCreate/$', CreateView.as_view(model=myModel)),
)

使用 mymodel_form.html 模板,例如:

<form method="post" action="">
{% csrf_token %}
  {{ form.as_p }}
  <input type="submit" value="Submit" />
</form>

当我提交表单时,会创建新对象,但出现错误

配置不正确...

没有网址 重定向到.要么提供一个网址,要么 定义一个 get_absolute_url 方法 模型。

如何指定成功时重定向的 url?

I'm using the generic CreateView like:

#urls.py

from django.conf.urls.defaults import *
from django.views.generic import CreateView
from content.models import myModel

urlpatterns = patterns('myApp.views',
    (r'myCreate/

With a mymodel_form.html template like:

<form method="post" action="">
{% csrf_token %}
  {{ form.as_p }}
  <input type="submit" value="Submit" />
</form>

When I submit my form, the new object is created but I get the error

ImproperlyConfigured at ...

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

How can I specify the url to redirect on success ?

, CreateView.as_view(model=myModel)), )

With a mymodel_form.html template like:

When I submit my form, the new object is created but I get the error

ImproperlyConfigured at ...

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

How can I specify the url to redirect on success ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

疯了 2024-11-21 07:05:12

您是否尝试过传入 success_url ?例如

CreateView.as_view(model=myModel, success_url="/success/")

,或者如果您想重定向到命名视图:

CreateView.as_view(model=myModel, success_url=reverse('success-url'))

Have you tried passing in success_url? e.g.

CreateView.as_view(model=myModel, success_url="/success/")

or if you want to redirect to a named view:

CreateView.as_view(model=myModel, success_url=reverse('success-url'))
乜一 2024-11-21 07:05:12

您还可以尝试在模型中定义 get_absolute_url 。例如

class Something(models.Model):
    name = models.CharField(max_length=50, verbose_name='name')

    class Meta:
        pass

    def get_absolute_url(self):
        return u'/some_url/%d' % self.id 

you can also try to define get_absolute_url in your models. For example

class Something(models.Model):
    name = models.CharField(max_length=50, verbose_name='name')

    class Meta:
        pass

    def get_absolute_url(self):
        return u'/some_url/%d' % self.id 
丿*梦醉红颜 2024-11-21 07:05:12

添加视图:

def form_valid(self, form):

   return self.render_to_response(self.get_context_data(form=form))

Add in views:

def form_valid(self, form):

   return self.render_to_response(self.get_context_data(form=form))
青巷忧颜 2024-11-21 07:05:12

views.py/

from django.views.generic import CreateView
from .models import myModel

class createView(CreateView):
    model = myModel
    fields = ['your fields', ...'']

    def get_success_url(self):
        return reverse('some_url')

urls.py/ (您​​的应用程序的)

from django.urls import path
from . import views

urlpatterns = [
    path('create/', views.createView.as_view(), name='create') 
] 

views.py/

from django.views.generic import CreateView
from .models import myModel

class createView(CreateView):
    model = myModel
    fields = ['your fields', ...'']

    def get_success_url(self):
        return reverse('some_url')

urls.py/ (of your app)

from django.urls import path
from . import views

urlpatterns = [
    path('create/', views.createView.as_view(), name='create') 
] 
孤城病女 2024-11-21 07:05:12
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    success_url = '/'
    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = Post
    success_url = '/'
    def test_func(self):
        post = self.get_object()
        if self.request.user == post.author:
            return True
        return False
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文