django - 如何使用 django 中的模板标签在另一个应用程序中调用视图?
问题描述
我收到一个错误,找不到“question_list”的反向。“question_list”不是有效的视图函数或模式名称。在模板中使用模板标签“collab_app:question_list”后。
主页.html:
{% extends "_base.html" %}
{% load static %}
{% load socialaccount %}
{% block title %}Home{% endblock title %}
{% block content %}
<h1>Homepage</h1>
<a href="{% url 'collab_app:question_list' %}">Ask Question Here</a>
<img class="collabimage" src="{% static 'images/collab.jpg' %}" ><br>
{% if user.is_authenticated %}
Hi {{ user.email }}
<p><a href="{% url 'account_logout' %}">Log Out</a></p>
{% else %}
<p>You are not logged in</p>
<!--Github-->
<a href="{% provider_login_url 'github' %}" ><p class="git">Github</p></a>
<a href="{% url 'account_login' %}">Login</a>
<a href="{% url 'account_signup' %}">Sign Up</a>
{% endif %}
{% endblock content %}
视图.py:
class HomePageView(generic.TemplateView):
template_name = "home.html"
用户应用程序中的 urls.py:
from django.urls import path, include
from .views import SignupPageView
from .views import HomePageView
app_name = "users"
urlpatterns = [
path("", HomePageView.as_view(), name="home"),
path("signup/", SignupPageView.as_view(), name="signup"),
]
项目中的 urls.py:
from django.contrib import admin
from django.urls import path, include
from users.views import HomePageView
urlpatterns = [
path("", include("users.urls")),
path("collab/", include("collab_app.urls"),), # , "collab_app")),
]
collap_app 中的 urls.py:
from django.contrib import admin
from django.urls import path, include
from collab_app import views
from users.views import HomePageView
app_name = "collab_app"
urlpatterns = [
path("", views.QuestionListView.as_view(), name="question-list"),
]
collab_app 中的 view.py:
class QuestionListView(ListView):
model = Question
template_name = "collab_app/question_list.html"
解决方案
您将网址命名为question-list
并且您正在引用question_list
;这只是一个错字。
改为使用{% url 'collab_app:question-list' %}
。
推荐阅读
- android - 是否有 NEXT 或 CONTINUE 的 NATIVE ANDROID 按钮?如果是这样,它看起来像什么?
- xcode - iOS 登录应用程序无法在 appcenter 上运行
- java - Spring 模块应用程序属性
- java - Jhipster JPA - 过期后删除实体
- python - 在heroku上部署的Django web-app上没有css样式
- blockchain - 如何在corda中进行多层查询,从一种模式到另一种模式
- angular - Angular 多重过滤 Observable,rxJS
- python - pd.to_cvs 用空格替换逗号
- angular - 使用 Firebase 身份验证注册时如何为用户存储其他数据?
- python - 如何全局编辑变量python