Django 1.8.3 urls
我刚接触Django&;我使用的是1.8.3版。我的网址有问题。我到处都找不到答案。无论我做什么,我总是得到一个"nameerror",当我没有得到一个错误时,我只得到index.html页面。
两个
我希望
我在questions/admin.py中注册了模型,并在myproj/settings.py中安装了应用程序。
文件夹结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | MyProj |.Python |_bin |_include |_lib |_src | |_questions | | |_ __init__.py | | |_admin.py | | |_migrations | | |_models.py | | |_tests.py | | |_urls.py | | |_views.py | | | |_MyProj | | |_ __init__.py | | |_settings.py | | |_urls.py | | |_views.py | | |_wsgi.py | | | |_static_in_pro | | | | | |_our_static | | |_bootstrap | | |_css | | |_fonts | | |_js | | | |_templates | | |_base.html | | |_index.html | | |_errors | | |_partials | | |_questions | | |_home.html | | | |_vendor | |_static_in_env |_media_root |_static_root |
myproj/设置.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | . . . # Application definition INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', #third party apps 'crispy_forms', 'localflavor', 'registration', #my apps # 'jobs', # 'likes', # 'matches', # 'newsletter', # 'profiles', 'questions', ) . . . TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR,"templates")], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] . . . # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.8/howto/static-files/ STATIC_URL = '/static/' STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static_in_env","static_root") STATICFILES_DIRS = ( os.path.join(BASE_DIR,"static_in_pro","our_static"), . . . |
号
myproj/urls.py版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | from django.conf.urls import include, url from django.contrib import admin from django.conf import settings from django.conf.urls.static import static from questions import views, urls from .views import IndexView urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url('^.*$', IndexView.as_view(), name='index'), # url(r'^$', include('questions.urls') ), #Changed here url(r'^questions/$', views.home, name='home'), #url(r'^questions/$', include(questions.urls)), ] if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) |
我的项目/视图.py
1 2 3 4 5 6 7 | from django.shortcuts import render from django.views.generic.base import TemplateView from questions import views class IndexView(TemplateView): template_name = 'index.html' |
。
问题/urls.py
1 2 3 4 5 6 7 8 | from django.conf.urls import patterns, url from questions import views urlpatterns = [ url(r'^questions$', views.home, name='home'), ] |
问题/管理.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | from django.contrib import admin # Register your models here. from .models import Question, Answer class AnswerTabularInline(admin.TabularInline): model = Answer class QuestionAdmin(admin.ModelAdmin): inlines = [AnswerTabularInline] class Meta: model = Question admin.site.register(Question, QuestionAdmin) admin.site.register(Answer) |
。
问题/视图.py
1 2 3 4 5 6 7 8 9 10 | from django.shortcuts import render from django.http import Http404 from django.shortcuts import render, get_object_or_404, redirect # Create your views here. from .models import Question, Answer def home(request): return render(request,"questions.home.html", {}) |
。
有人能告诉我我遗漏了什么吗?事先谢谢!:)
您的URL模式
1 | url('^.*$', IndexView.as_view(), name='index'), |
要只匹配索引(即
1 | url(r'^$', IndexView.as_view(), name='index'), |
号
注意,我也在regex中添加了