【问题标题】:How to create href link from one Django app to another如何创建从一个 Django 应用程序到另一个应用程序的 href 链接
【发布时间】:2021-06-19 21:05:10
【问题描述】:

我知道这已经被问过多次了,但我还是不明白。
如何在一个 Django 应用中创建一个将用户发送到第二个应用的 href 链接?

这是我的设置:

myproject/
  manage.py
  myproject/
    __init__.py
    settings.py
    urls.py
    asgi.py
    wsgi.py

  app1/
    migrations/
    templates/
      app1/
        app1.html
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py

  app2/
    migrations/
    templates/
      app2/
        app2.html
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py

我的项目/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app1/', include('app1.urls'), name='app1'),
    path('app2/', include('app2.urls'), name='app2'),
]

我的项目/settings.py

INSTALLED_APPS = [
    'app1.apps.App1Config',
    'app2.apps.App2Config',
    'django.contrib.admin',
...
]

app1/urls.py(类似于app2)

from django.urls import path
from . import views

urlpatterns = [
    path('', views.app1, name='app1'),
    ]

app1/views.py(类似于 app2)

from django.template import loader
from django.http import HttpResponse

def app1(request):
    template = loader.get_template('app1/app1.html')
    return HttpResponse(template.render())

app1/templates/app1/app1.html(类似于app2)

<p>This is the template of app1.</p>
<a href="app2/">Go to App2</a> # doesn't work
<a href="app2/app2.html">Go to App2</a> # doesn't work
<a href="./app2/app2.html">Go to App2</a> # doesn't work

我的问题是,这会将我发送到地址 127.0.0.1:8000/app1/app2/,这给了我一个 404 错误。不过,我可以通过127.0.0.1:8000/app2/ 毫无问题地访问app2。

编辑:
感谢大家的反馈。在考虑了您的建议后,我进行了以下更改:

我的项目/urls.py:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app1/', include('app1.urls', namespace='app1')), # ADDED NAMESPACE
    path('app2/', include('app2.urls', namespace='app2')), # ADDED NAMESPACE
]

myproject/settings.py:没有变化

app1/urls.py:(类似于app2)

from django.urls import path
from . import views

app_name = 'app1' # THIS WAS MISSING
urlpatterns = [
    path('', views.app1, name='app1'),
    ]

app1/views.py:没有变化

app1/templates/app1/app1.html:(类似于app2)

<p>This is the template of app1.</p>
<a href="{% url 'app2:app2' %}">Go to App2</a> # AS SUGGESTED BY seif

现在可以了。

【问题讨论】:

  • 它们在不同的端口上运行吗?
  • 我不认为问题出在不同的端口上。更多模板相关

标签: django


【解决方案1】:

您的 href 链接不会转到模板中的 .html 文件,模板仅适用于 UI,并且对您的项目如何路由几乎没有发言权,实际上这取决于您的配置。相反,它应该转到您的 url 文件中指定的 url,

所以说你的基地网址是

path('app1/', include('app1.urls'), name='app1')

对于您的一个应用程序,它将是

<a href="example.com/app1">App 1</a>

如果你在app1 url文件下还有其他链接,那就是

<a href="example.com/app1/myother-app1-link">A link under App 1</a>

example.com 在这里,可以是 localhost:8000 或者只是带有任何端口的 localhost,这取决于您的 django 项目运行的端口。

那么至于这个,

from django.template import loader
from django.http import HttpResponse

def app1(request):
    template = loader.get_template('app1/app1.html')
    return HttpResponse(template.render())

我会建议你这样做

from django.shortcuts import render
from django.http import HttpResponse, HttpRequest

def app1(request,slug=None):
    return render(request,'app1/app1.html',{})
    #path to the template folder, under templates, depends on your configuration though

请阅读更多关于 Django 中的模板配置和路由,但这应该是一个快速入门。

干杯

【讨论】:

    【解决方案2】:

    在您的模板和视图中尽量不要硬编码 href 的 url,而是使用 {%url ''%} 这将是{%url 'namespace:urlname'%}

    <p>This is the template of app1.</p>
    <a href="{% url 'app2:app2'%}">Go to App2</a>
    

    这使您可以灵活地更改 url 中的路径,而不必担心在所有模板和视图中更改它,您可以在此处阅读更多信息 https://docs.djangoproject.com/en/3.1/topics/http/urls/#reverse-resolution-of-urls

    【讨论】:

      【解决方案3】:

      当您输入“127.0.0.1:8000/app1/app2/”时,django 将收到“app1/app2/”并如下工作(不准确):

      1.将 app1/ 与 myproject/urls.py 的 urlpatterns 中的每个元素进行比较

      所以,app1/ 将会被找到。

      2.将 app2/ 与 app1.urls 的 urlpatterns 中的每个元素进行比较

      什么都找不到,因为app1.urls的urlpatterns中只有一个“”。

      所以你得到一个 404 页面。

      如果输入“127.0.0.1:8000/app2/”,在第2步,“”等于“”,

      函数views.app2 执行,你得到一个正确的页面。

      我建议你阅读官方网站上的 django 教程。

      https://docs.djangoproject.com/en/3.1/intro/tutorial01/

      【讨论】:

        猜你喜欢
        • 2021-04-28
        • 2014-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 2019-04-25
        • 1970-01-01
        相关资源
        最近更新 更多