2015-10-24 57 views
1

請幫我學習django的明星。我試圖理解爲什麼我得到404錯誤。django第一個項目

還有就是我的主urls.py

from django.conf.urls import include, url 
from django.contrib import admin 
admin.autodiscover() 

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^/', include('account_app.urls')), 
] 

我的帳戶urls.py 從django.conf.urls導入模式,網址

from account_app import views 

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

還有就是我的戶頭的觀點

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

from yes_no.models import account_class # added to model yes_no 


def index(request): 
    template = loader.get_template('./index.html') 
    context = RequestContext(request, { 
     'a' : a, 
    }) 
    return HttpResponse(template.render(context)) 

PS我的index.html文件存在於會話視圖和網址文件所在的文件夾中

感謝您的幫助。

+1

應該在模板文件夾在您的應用程序或項目,檢查django文檔 – user996142

+0

什麼是?!你必須初始化一個變量。 – MHossein

+1

您的404由包含網址格式中的前導斜槓引起。然而,正如其他人指出的那樣,還有很多其他的事情,特別是你放置模板的地方以及你如何引用它。 –

回答

0

也許你需要開始使用Class Based View?所有的事情變得更容易。

urls.py應該是這樣的

from django.conf import settings 
from django.views.static import serve 
from some_app.views import HomePageView(

urlpatterns = [ 
    url(r'^about/', HomePageView(.as_view()), 
] 

if settings.DEBUG: 
    urlpatterns += [ 
     url(r'^media/(?P<path>.*)$', serve, { 
      'document_root': settings.MEDIA_ROOT, 
     }), 
    ] 

views.py

from django.views.generic.base import TemplateView 

class HomePageView(TemplateView): 
    template_name = "home.html" 
    def get(self, request, *args, **kwargs): 
     context = dict() 
     context['foo'] = 'bar' 
     return Homepage.render_to_response(self, context) 

我們需要完整的跟蹤錯誤