URL "http://localhost:8000/buy/" 没有任何显示
Nothing showing up at URL "http://localhost:8000/buy/"
我想在我的网站上包含一个电子商务门户,我已经完成了 Django-Oscar 文档中给出的 "Create your Shop page" 中给出的所有内容,只是在
urls.py 而不是
url(r'', include(application.urls)),
我已添加
url(r'^buy/', include(application.urls)),
但问题是当我点击 url 它既没有显示任何内容也没有给出错误。
知道可能是什么问题吗?
可能是某些 url 发生冲突或其他一些微不足道的问题,但我无法理解从哪里开始调试。
文件urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from view import AboutusView, TwentySevenView
import apps.users.views
from django.conf import settings
#oscar urls
from oscar.app import application
admin.site.site_header = ''
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'torquehp.views.home', name='home'),
# url(r'^users/', include('users.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', include(admin.site.urls)),
url(r'^about-us/$', AboutusView.as_view(), name="about-us"),
url(r'^24x7/$', TwentySevenView.as_view(), name="24x7"),
url(r'^$', apps.users.views.user_home, name="home"),
url(r'^markdown/', include('django_markdown.urls')),
url(r'^users/', include('apps.users.urls')),
url(r'^doorstep/', include('apps.doorstep.urls')),
url(r'^cars/', include('apps.cars.urls')),
url('', include('social.apps.django_app.urls', namespace='social')),
# urls for zinnia
url(r'^blog/', include('zinnia.urls', namespace='zinnia')),
url(r'^comments/', include('django_comments.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, }),
# oscar URLs
url(r'^buy/', include(application.urls)),
)
文件settings.py
import os
import sys
from oscar import get_core_apps
from oscar import OSCAR_MAIN_TEMPLATE_DIR
from oscar.defaults import *
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
location = lambda x: os.path.join(
os.path.dirname(os.path.realpath(__file__)), x)
# sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) # to store apps in apps/ directory
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'apps.users',
'apps.doorstep',
'apps.cars',
'django_markdown',
'social.apps.django_app.default',
'django.contrib.sites',
'django_comments',
'mptt',
'tagging',
'zinnia',
'compressor',
] + get_core_apps()
# specifying the comments app
# COMMENTS_APP = 'zinnia_threaded_comments'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'oscar.apps.basket.middleware.BasketMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
# Authentication backend used by python-social-auth
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
'oscar.apps.customer.auth_backends.EmailBackend',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"social.apps.django_app.context_processors.backends",
"social.apps.django_app.context_processors.login_redirect",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.i18n",
"django.core.context_processors.request",
"zinnia.context_processors.version",
"django.core.context_processors.debug",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata'
)
# TEMPLATE_LOADERS = (
# 'app_namespace.Loader',
# )
ROOT_URLCONF = 'torquehp.urls'
WSGI_APPLICATION = 'torquehp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "",
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306'
}
}
# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOGIN_URL = '/users/login/'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates').replace('\', '/'),
location('templates'),
OSCAR_MAIN_TEMPLATE_DIR,
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
REGISTRATION_INVALID = 50
MESSAGE_TAGS = {
REGISTRATION_INVALID: 'InvalidDetails',
}
# temporary , should not be in settings file
SOCIAL_AUTH_FACEBOOK_KEY = ""
SOCIAL_AUTH_FACEBOOK_SECRET = ""
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ""
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ""
LOGIN_REDIRECT_URL = '/blog/'
# ------------------------------------ #
# Settings for sending email #
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# ------------------------------------ #
# site id for django sites framework
SITE_ID = 1
# configuration settings for django-zinnia
ZINNIA_PING_EXTERNAL_URLS = False
ZINNIA_SAVE_PING_DIRECTORIES = False
ZINNIA_MARKUP_LANGUAGE = 'markdown'
ZINNIA_UPLOAD_TO = 'uploads/zinnia/'
# TimeZone Settings
USE_TZ = True
# ------------------------ #
# django s=oscar search settings
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
}
截图:
http://localhost:8000/buy/catalogue/
urls.py
url(r'^buy/', include("app.urls")),
然后在你的应用目录下创建一个名为urls.py
的文件
app/urls.py
# this is your “http://localhost:8000/buy/”
url(r'^$', ViewName.as_view(), name="thename"),
在 app/urls.py 中添加以下行:
url(r'^$',views.index,name='index'),
这会将您重定向到 app/views.py 中的索引函数
在 app/views.py 中做这样的事情:
def index(request):
#Your logic here
return render(request,'urpoll/index.html'{'category':categorys,'ques':ques,'url':url})
这将呈现 index.html 类别、问题、url。在 index.HTML 中是这样的:
{{ if category }}
{{ category }}
{{ endif }}
希望对您有所帮助。
我看到在你的第二张截图中 localhost:8000/buy/catalogue/
你实际上得到了一个结果。看起来好像您还没有定义自己的模板,或者需要以某种方式修复您的 TEMPLATE_DIRS
或 TEMPLATE_CONTEXT_PROCESSORS
设置。
我没用过 oscar,但你在屏幕上看到的东西对我来说似乎是模板加载问题。
See their docs about setting the template_context_processors
你能post你的设置吗?
更新:
查看您的设置后,template_context_processors 看起来是正确的。接下来我查看了 oscar 的模板,它们似乎在没有限定 oscar 目录的情况下进行了扩展。例如,layout.html (/oscar/templates/oscar/layout.html) 这样做:
{% extends 'base.html' %}
现在,如果您在自己的项目目录中也有一个 'base.html' 文件,比如 project_dir/templates/base.html,那么它可能会改用它。您能否尝试暂时重命名该文件(如果存在)并看看会发生什么?
可能您与模板的命名冲突。 Django 的默认模板加载器(我假设您正在使用)可能有点不直观。当您要求它提供具有给定名称的模板时,它会按顺序遍历设置中的 INSTALLED_APPS
元组,并在每个元组中检查一个模板文件夹。如果在两个不同的应用程序中有两个具有相同路径的模板,它将始终使用它找到的第一个。
这就是为什么 django docs 中的建议是 'namespace' 您的模板,方法是将它们全部保存在模板文件夹中与您的应用程序名称相同的另一个文件夹中。 Oscar 这样做,所以他们的模板在 oscar/templates/oscar
中,所以他们不应该与任何其他应用程序冲突,但我想知道你是否可以在你的其中一个模板中放置一个 'oscar' 文件夹应用程序,可能与 base.html
.
冲突
问题是 django-oscar 不应该包含在子路径下,例如 /buy/
。在他们的文档中,他们的示例显示将其安装为:
url(r'', include(application.urls)),
如果您查看 get_urls()
的 the source,它为 application.urls
提供了 return 值,您可以看到它没有提供 urlpattern 来匹配 r'^$'
,这是您需要在您尝试查看的 URL 处匹配的内容。
简而言之,您应该完全按照文档的建议进行操作,否则您会得到意想不到的结果。
我想在我的网站上包含一个电子商务门户,我已经完成了 Django-Oscar 文档中给出的 "Create your Shop page" 中给出的所有内容,只是在 urls.py 而不是
url(r'', include(application.urls)),
我已添加
url(r'^buy/', include(application.urls)),
但问题是当我点击 url 它既没有显示任何内容也没有给出错误。
知道可能是什么问题吗?
可能是某些 url 发生冲突或其他一些微不足道的问题,但我无法理解从哪里开始调试。
文件urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from view import AboutusView, TwentySevenView
import apps.users.views
from django.conf import settings
#oscar urls
from oscar.app import application
admin.site.site_header = ''
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'torquehp.views.home', name='home'),
# url(r'^users/', include('users.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^admin/', include(admin.site.urls)),
url(r'^about-us/$', AboutusView.as_view(), name="about-us"),
url(r'^24x7/$', TwentySevenView.as_view(), name="24x7"),
url(r'^$', apps.users.views.user_home, name="home"),
url(r'^markdown/', include('django_markdown.urls')),
url(r'^users/', include('apps.users.urls')),
url(r'^doorstep/', include('apps.doorstep.urls')),
url(r'^cars/', include('apps.cars.urls')),
url('', include('social.apps.django_app.urls', namespace='social')),
# urls for zinnia
url(r'^blog/', include('zinnia.urls', namespace='zinnia')),
url(r'^comments/', include('django_comments.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, }),
# oscar URLs
url(r'^buy/', include(application.urls)),
)
文件settings.py
import os
import sys
from oscar import get_core_apps
from oscar import OSCAR_MAIN_TEMPLATE_DIR
from oscar.defaults import *
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
location = lambda x: os.path.join(
os.path.dirname(os.path.realpath(__file__)), x)
# sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) # to store apps in apps/ directory
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ''
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'apps.users',
'apps.doorstep',
'apps.cars',
'django_markdown',
'social.apps.django_app.default',
'django.contrib.sites',
'django_comments',
'mptt',
'tagging',
'zinnia',
'compressor',
] + get_core_apps()
# specifying the comments app
# COMMENTS_APP = 'zinnia_threaded_comments'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'oscar.apps.basket.middleware.BasketMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)
# Authentication backend used by python-social-auth
AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'social.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
'oscar.apps.customer.auth_backends.EmailBackend',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"social.apps.django_app.context_processors.backends",
"social.apps.django_app.context_processors.login_redirect",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.i18n",
"django.core.context_processors.request",
"zinnia.context_processors.version",
"django.core.context_processors.debug",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata'
)
# TEMPLATE_LOADERS = (
# 'app_namespace.Loader',
# )
ROOT_URLCONF = 'torquehp.urls'
WSGI_APPLICATION = 'torquehp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': "",
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306'
}
}
# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOGIN_URL = '/users/login/'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates').replace('\', '/'),
location('templates'),
OSCAR_MAIN_TEMPLATE_DIR,
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'staticfiles'),
)
REGISTRATION_INVALID = 50
MESSAGE_TAGS = {
REGISTRATION_INVALID: 'InvalidDetails',
}
# temporary , should not be in settings file
SOCIAL_AUTH_FACEBOOK_KEY = ""
SOCIAL_AUTH_FACEBOOK_SECRET = ""
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ""
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ""
LOGIN_REDIRECT_URL = '/blog/'
# ------------------------------------ #
# Settings for sending email #
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
SERVER_EMAIL = EMAIL_HOST_USER
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# ------------------------------------ #
# site id for django sites framework
SITE_ID = 1
# configuration settings for django-zinnia
ZINNIA_PING_EXTERNAL_URLS = False
ZINNIA_SAVE_PING_DIRECTORIES = False
ZINNIA_MARKUP_LANGUAGE = 'markdown'
ZINNIA_UPLOAD_TO = 'uploads/zinnia/'
# TimeZone Settings
USE_TZ = True
# ------------------------ #
# django s=oscar search settings
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
}
截图:
http://localhost:8000/buy/catalogue/
urls.py
url(r'^buy/', include("app.urls")),
然后在你的应用目录下创建一个名为urls.py
app/urls.py
# this is your “http://localhost:8000/buy/”
url(r'^$', ViewName.as_view(), name="thename"),
在 app/urls.py 中添加以下行:
url(r'^$',views.index,name='index'),
这会将您重定向到 app/views.py 中的索引函数 在 app/views.py 中做这样的事情:
def index(request):
#Your logic here
return render(request,'urpoll/index.html'{'category':categorys,'ques':ques,'url':url})
这将呈现 index.html 类别、问题、url。在 index.HTML 中是这样的:
{{ if category }}
{{ category }}
{{ endif }}
希望对您有所帮助。
我看到在你的第二张截图中 localhost:8000/buy/catalogue/
你实际上得到了一个结果。看起来好像您还没有定义自己的模板,或者需要以某种方式修复您的 TEMPLATE_DIRS
或 TEMPLATE_CONTEXT_PROCESSORS
设置。
我没用过 oscar,但你在屏幕上看到的东西对我来说似乎是模板加载问题。
See their docs about setting the template_context_processors
你能post你的设置吗?
更新:
查看您的设置后,template_context_processors 看起来是正确的。接下来我查看了 oscar 的模板,它们似乎在没有限定 oscar 目录的情况下进行了扩展。例如,layout.html (/oscar/templates/oscar/layout.html) 这样做:
{% extends 'base.html' %}
现在,如果您在自己的项目目录中也有一个 'base.html' 文件,比如 project_dir/templates/base.html,那么它可能会改用它。您能否尝试暂时重命名该文件(如果存在)并看看会发生什么?
可能您与模板的命名冲突。 Django 的默认模板加载器(我假设您正在使用)可能有点不直观。当您要求它提供具有给定名称的模板时,它会按顺序遍历设置中的 INSTALLED_APPS
元组,并在每个元组中检查一个模板文件夹。如果在两个不同的应用程序中有两个具有相同路径的模板,它将始终使用它找到的第一个。
这就是为什么 django docs 中的建议是 'namespace' 您的模板,方法是将它们全部保存在模板文件夹中与您的应用程序名称相同的另一个文件夹中。 Oscar 这样做,所以他们的模板在 oscar/templates/oscar
中,所以他们不应该与任何其他应用程序冲突,但我想知道你是否可以在你的其中一个模板中放置一个 'oscar' 文件夹应用程序,可能与 base.html
.
问题是 django-oscar 不应该包含在子路径下,例如 /buy/
。在他们的文档中,他们的示例显示将其安装为:
url(r'', include(application.urls)),
如果您查看 get_urls()
的 the source,它为 application.urls
提供了 return 值,您可以看到它没有提供 urlpattern 来匹配 r'^$'
,这是您需要在您尝试查看的 URL 处匹配的内容。
简而言之,您应该完全按照文档的建议进行操作,否则您会得到意想不到的结果。