带有子域的 Django 编写视图
Django writing view with sudomain
我正在使用我的 (Django v 1.17) 项目 django-subdomains。
我可以毫无问题地调用索引视图,当我打开 url https://subdomain.domain.com 时,我会得到 index.html。
我的问题是我为子域编写了一个名为 example 的新视图,但是当我打开 url https://subdomain.domain.com/exmaple 时,我将收到错误页面未找到 (404)。
Hete是我的代码:
settings.py
INSTALLED_APPS = [
'subdomain'
]
SUBDOMAIN_URLCONFS = {
'subdomain': 'subdomain.urls',
}
subdomain/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'),
]
subdomain/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.
请告知如何解决此问题并为子域编写视图。
这与 django-subdomains 无关。美元应该在正则表达式的 end。
url(r'^example$', views.example, name='example'),
美元与字符串的末尾相匹配,因此如果它位于开头则不会匹配。
我正在使用我的 (Django v 1.17) 项目 django-subdomains。 我可以毫无问题地调用索引视图,当我打开 url https://subdomain.domain.com 时,我会得到 index.html。 我的问题是我为子域编写了一个名为 example 的新视图,但是当我打开 url https://subdomain.domain.com/exmaple 时,我将收到错误页面未找到 (404)。 Hete是我的代码:
settings.py
INSTALLED_APPS = [
'subdomain'
]
SUBDOMAIN_URLCONFS = {
'subdomain': 'subdomain.urls',
}
subdomain/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'),
]
subdomain/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.
请告知如何解决此问题并为子域编写视图。
这与 django-subdomains 无关。美元应该在正则表达式的 end。
url(r'^example$', views.example, name='example'),
美元与字符串的末尾相匹配,因此如果它位于开头则不会匹配。