• Django 解决No URL to redirect to.


     

    1. Internal Server Error: /app3/books/new/
    2. Traceback (most recent call last):
    3. File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 116, in get_success_url
    4. url = self.object.get_absolute_url()
    5. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    6. AttributeError: 'Book' object has no attribute 'get_absolute_url'
    7. During handling of the above exception, another exception occurred:
    8. Traceback (most recent call last):
    9. File "D:\py\Lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    10. response = get_response(request)
    11. ^^^^^^^^^^^^^^^^^^^^^
    12. File "D:\py\Lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    13. response = wrapped_callback(request, *callback_args, **callback_kwargs)
    14. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    15. File "D:\py\Lib\site-packages\django\views\generic\base.py", line 70, in view
    16. return self.dispatch(request, *args, **kwargs)
    17. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    18. File "D:\py\Lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    19. return handler(request, *args, **kwargs)
    20. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    21. File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 172, in post
    22. return super().post(request, *args, **kwargs)
    23. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    24. File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 142, in post
    25. return self.form_valid(form)
    26. ^^^^^^^^^^^^^^^^^^^^^
    27. File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 126, in form_valid
    28. return super().form_valid(form)
    29. ^^^^^^^^^^^^^^^^^^^^^^^^
    30. File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 57, in form_valid
    31. return HttpResponseRedirect(self.get_success_url())
    32. ^^^^^^^^^^^^^^^^^^^^^^
    33. File "D:\py\Lib\site-packages\django\views\generic\edit.py", line 118, in get_success_url
    34. raise ImproperlyConfigured(
    35. 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

    1. from django.db import models
    2. # Create your models here.
    3. from django.urls import reverse
    4. class Book(models.Model):
    5. title = models.CharField(max_length=200)
    6. author = models.CharField(max_length=100)
    7. publication_date = models.DateField()
    8. def get_absolute_url(self):
    9. 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

    1. from django.shortcuts import render
    2. # Create your views here.
    3. from .models import Book
    4. from django.views.generic import ListView
    5. class BookListView(ListView):
    6. model = Book
    7. context_object_name = 'books'
    8. template_name = 'books/book_list.html'
    9. paginate_by = 10 # 设置展示页数数据
    10. from django.views.generic import DetailView
    11. class BookDetailView(DetailView):
    12. model = Book
    13. context_object_name = 'book'
    14. template_name = 'books/book_detail.html'
    15. from django.views.generic.edit import CreateView
    16. class BookCreateView(CreateView):
    17. model = Book
    18. template_name = 'books/book_form.html'
    19. fields = ['title', 'author', 'publication_date']
    20. success_url = '/app3/books/' # 重定向至书本列表路由地址

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

  • 相关阅读:
    Spring中JDK与Cglib动态代理的区别
    k8s加固 hardening
    HTML做一个学校网站(纯html代码)
    vue3.x+ts项目创建,配置流程
    帮助开放大学学子们更好学习的铺助工具
    StringUtils 工具类常用方法汇总 1(判空、转换、移除、替换、反转)
    【复杂网络】网络科学导论学习笔记-第四章度相关性和社团结构
    【路径规划】基于遗传算法求解固定的开放式多旅行推销员问题(M-TSP)附matlab代码
    数字IC设计笔试题汇总(四):一些基础知识点
    释放搜索潜力:基于ES(ElasticSearch)打造高效的语义搜索系统,让信息尽在掌握[1.安装部署篇--简洁版],支持Linux/Windows部署安装
  • 原文地址:https://blog.csdn.net/qq_26086231/article/details/139609469