Django 解决No URL to redirect to.

 

Internal Server Error: /app3/books/new/
Traceback (most recent call last):
  File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 116, in get_success_url
    url = self.object.get_absolute_url()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Book' object has no attribute 'get_absolute_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\py\Lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
           ^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 126, in form_valid
    return super().form_valid(form)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 57, in form_valid
    return HttpResponseRedirect(self.get_success_url())
                                ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 118, in get_success_url
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.

这个错误表明Django在尝试重定向到新创建的对象的详情页面时找不到要去的URL。你有两种方式来解决这个问题:

方法一

  1. 在模型中定义一个get_absolute_url方法。这个方法应该返回一个URL,这个URL指向对象的详情页面。例如:

 Test/app3/models.py

from django.db import models

# Create your models here.
from django.urls import reverse
class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.CharField(max_length=100)
    publication_date = models.DateField()


    def get_absolute_url(self):
        return reverse('book_detail', args=[str(self.id)])

在这个例子中,get_absolute_url方法返回的URL是由名称为book_detail的URL模式生成的,这个URL模式应该接受一个参数(这里是self.id,也就是书籍的ID)。你需要在你的urls.py文件中定义一个名称为book_detail的URL模式。

方法二

Test/app3/views.py

from django.shortcuts import render

# Create your views here.
from .models import Book

from django.views.generic import ListView
class BookListView(ListView):
    model = Book
    context_object_name = 'books'
    template_name = 'books/book_list.html'
    paginate_by = 10 # 设置展示页数数据


from django.views.generic import DetailView
class BookDetailView(DetailView):
    model = Book
    context_object_name = 'book'
    template_name = 'books/book_detail.html'


from django.views.generic.edit import CreateView
class BookCreateView(CreateView):
    model = Book
    template_name = 'books/book_form.html'
    fields = ['title', 'author', 'publication_date']
    success_url = '/app3/books/' # 重定向至书本列表路由地址

 保存书籍后重定向至http://127.0.0.1:8000/app3/books/

相关推荐

  1. Python Django相关解答

    2024-06-12 08:18:04       16 阅读
  2. Djange解决跨域问题

    2024-06-12 08:18:04       13 阅读
  3. Django解决跨域问题

    2024-06-12 08:18:04       37 阅读
  4. 解决django跨域问题详解

    2024-06-12 08:18:04       19 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 08:18:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 08:18:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 08:18:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 08:18:04       18 阅读

热门阅读

  1. 14、架构-可靠通讯之零信任网络

    2024-06-12 08:18:04       6 阅读
  2. 如何在小程序中实现页面之间的返回

    2024-06-12 08:18:04       7 阅读
  3. Linux中“计划任务”设置以及补充

    2024-06-12 08:18:04       4 阅读
  4. Json和Protobuf区别详细分析

    2024-06-12 08:18:04       5 阅读
  5. 一五二、go缓存GCache和Go-Redis

    2024-06-12 08:18:04       6 阅读
  6. react 0至1 【jsx】

    2024-06-12 08:18:04       6 阅读
  7. 一个有趣的c++案例

    2024-06-12 08:18:04       6 阅读
  8. “手撕”二叉树的OJ习题

    2024-06-12 08:18:04       6 阅读