0

在我的(Django v 1.17)项目中,我正在使用django-subdomains。我调用索引视图没有问题,当我打开我的网址https://subdomain.domain.com时,我会得到 index.html。我的问题是我为子域编写了一个名为 example 的新视图,但是当我打开 url https://subdomain.domain.com/exmaple时,我会收到错误 Page not found (404)。Hete是我的代码:

设置.py

INSTALLED_APPS = [
   'subdomain'
]
SUBDOMAIN_URLCONFS = {

    'subdomain': 'subdomain.urls',

}

子域/urls.py

 from django.conf.urls import url, include

from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^$example', views.example, name='example'),
]

子域/views.py

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

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


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

错误:

    Page not found (404)
    Request Method: GET
    Request URL:    https://subdomain.domain.com/example
    Using the URLconf defined in subdomain.urls, Django tried these URL patterns, in this order:
   1. ^$ [name='index']
   2. ^$example [name='example']

    The current path, econ, didn't match any of these. 

请告知如何解决此问题并为子域编写视图。

4

1 回答 1

1

这与 django-subdomains 无关。美元应该在正则表达式的末尾

url(r'^example$', views.example, name='example'),

美元匹配字符串的结尾,所以如果你在开头有它,那么它不会匹配。

于 2017-12-08T16:56:50.923 回答