【问题标题】:Django redirecting URL view for the latest postDjango 为最新帖子重定向 URL 视图
【发布时间】:2021-01-06 20:53:21
【问题描述】:

我是 Django 的新手,如果这看起来相当琐碎,请原谅。

我正在尝试创建指向在线杂志最新一期的链接。这是我的代码:

home.html:

{%extends 'base.html'%}
{%load static%}
{% block content%}       
<button href="#"> Latest Issue</button>
{%endblock%}

views.py:

from django.shortcuts import render, get_object_or_404
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post, Issue
from .forms import PostForm, EditForm, IssueForm, IssueEditForm
from django.urls import reverse_lazy
from django.http import HttpResponse

class HomeView(ListView):
    model = Issue
    template_name = "home.html"

class IssueView(ListView):
    model = Issue
    template_name='issues.html'

class IssueDetailView(DetailView):
    model = Issue
    template_name = 'issue_details.html'

    
    def get_context_data(self, **kwargs):
        context = super(IssueDetailView,self).get_context_data(**kwargs)
        context['posts'] = Post.objects.all()
        return context
#This is to pass in my post model, so I can display the posts for each issue in issue-details.html.
    

urls.py:

from django.urls import path
from .models import Post, Issue
#from . import views
from .views import HomeView, ArticleDetailView, AddPostView,EditPostView, DeletePostView,about_view, submit_view,IssueView, AddIssueView, IssueDetailView, EditIssueView

urlpatterns = [
    path('',HomeView.as_view(),name="home"),
   path('issue/',IssueView.as_view(), name="issue"), path('issue/<int:pk>', IssueDetailView.as_view(), name='issue-detail'),   
]
#Irrelevant urlpatterns omitted.

我查看了this solution,但我无法实现它,尤其是因为它是 9 年前的。我不知道为重定向 url “latest-issue”放置/创建什么视图,这是我认为我的问题所在。这是我的尝试:

\\home.html
<button href="{% url 'latest-issue'%}"> Latest Issue</button>

\\ urls.py
urlpatterns = [
   path('',HomeView.as_view(),name="home"),
   path('issue/',IssueView.as_view(), name="issue"), 
   path('issue/<int:pk>', IssueDetailView.as_view(), name='issue-detail'), 
   path('latest-issue/', latest-issue.redirector, name='latest-issue')
]
 
def redirector(request):
     issue = Issue.objects.latest('id')
     return http.HttpResponseRedirect(reverse('issue-detail', args=[issue.pk]))

我应该为重定向 url 放置什么视图,以便它仍然调用重定向函数?

【问题讨论】:

  • 嗨。 1)这个对象是什么? latest-issue.redirector 2) 当你运行页面时你得到了什么? 3) 看起来你需要一个单独的 url 路径和查看函数:'get-latest-issue' 它将检查最近的问题(仅在单击按钮时)并返回重定向
  • @Omri Shaffer 我试图复制我链接的解决方案,但我认为它不适用于我的情况。我会尝试你的方法,所以忽略我失败的尝试。我会在views.py中将“重定向器”函数中的代码设置为基于函数的视图“get_latest_issue”吗?我还需要添加什么吗?

标签: python django url


【解决方案1】:

感谢 Omri Shaffer 帮助我了解如何以不同的方式处理此问题。我在我的 urls.py 文件中创建了一个 url,它会提取一个基于函数的视图 get_latest_issue:

from .views import get_latest_issue
path('latest-issue/', get_latest_issue, name='latest-issue')

然后我在views.py中定义了“redirector”函数,确保导入正确的函数:

from django.shortcuts import HttpResponseRedirect
from django.urls import reverse

def get_latest_issue(request): 
     issue = Issue.objects.latest('id')
     return HttpResponseRedirect(reverse('issue-detail', args=[issue.pk]))

然后我更改了 home.html 中的按钮以使其成为链接,但我在第一次尝试时做错了:

<button onclick="location.href='{% url 'latest-issue' %}'"> Latest Issue</button>

我还可以使用 Bootstrap 的 btn 类将文本设置为链接并将其设置为按钮。

【讨论】:

  • 很高兴听到!
【解决方案2】:

如果我理解您的问题,您需要一个指向您模型中所有最新问题的链接吗? 您覆盖了问题视图中的默认查询集 queryset=modelname.objects.order_by('-created-time') 在您的模型中,您应该将 DateTimeField 设置为 auto-now-add=true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2021-06-16
    • 1970-01-01
    • 2020-08-10
    • 2015-11-01
    相关资源
    最近更新 更多