Django 1.8.3网址

时间:2015-12-11 14:50:06

标签: python django django-urls

我刚接触Django&我使用的是1.8.3版本。我在网址上遇到了麻烦。我到处寻找答案,找不到答案。无论我做什么,我都会收到“NameError”,当我没有收到错误时,我会得到index.html页面。

http://127.0.0.1:8000/& http://127.0.0.1:8000/questions将在模板目录中呈现index.html或抛出错误。

我希望http://127.0.0.1:8000/questions呈现home.html目录中的templates/questionshttp://127.0.0.1:8000/呈现index.html中的templates/

我在问题/ admin.py中注册了模型,并在MyProj / settings.py中安装了该应用程序。

文件夹结构:

 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 / settings.py


    . . .

    # 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


    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)

的Myproj / views.py


    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


    from django.conf.urls import patterns, url

    from questions import views

    urlpatterns = [
        url(r'^questions$', views.home, name='home'),

    ]

问题/ admin.py


    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)

问题/ views.py


    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", {})

有人可以告诉我我错过了什么吗?提前致谢! :)

2 个答案:

答案 0 :(得分:4)

您的网址格式index正在抓取所有网址。这意味着将忽略下面的任何网址模式。

url('^.*$', IndexView.as_view(), name='index'),

要仅匹配索引(即http://127.0.0.1:8000/),请将其更改为

url(r'^$', IndexView.as_view(), name='index'),

请注意,我还在正则表达式中添加了r''前缀。在这种情况下它没有任何区别,但在你的正则表达式中使用它是一个好习惯。

答案 1 :(得分:0)

在MyProj / urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url('^$', IndexView.as_view(), name='index'),
    url(r'^questions$', views.home, name='home'),
]

questions / urls.py:

 urlpatterns = [
    url(r'^$', views.home, name='home'),
]

questions / views.py

def home(request):
    return render(request, "questions/home.html", {})