2016-08-05 5 views
0

Djangoの新機能で、バージョン1.9.8を使用しています。私は公式のチュートリアルに従って、今はもっと進んだthisを試しています。私は「登録ユーザー」セクションの最後/チェックポイントにいます。私がhttp://localhost:8000/registerにアクセスしたとき、Djangoはstatic/templates/authentication/register.htmlにチュートリアルで作成されたものではなく、authentication/templates/authentication.htmlにある私のindex.htmlページにあるコンテンツを表示しています。DjangoのURLに問題があります

私が最初に私がどこに着いたとき、私はurls.pyに

#urls.py 
from django.conf.urls import include,url 
from django.contrib import admin 
from django.conf.urls import patterns 

from rest_framework_nested import routers 
from authentication.views import AccountViewSet 

router = routers.SimpleRouter() 
router.register(r'accounts', AccountViewSet) 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^api/v1/', include(router.urls)), 
    url('^.*$', IndexView.as_view(), name='index'), #this line was causing the error 
] 

を参照し、次のエラーImportError: cannot import name 'IndexView'を受けていた私は、同じチュートリアルを次れた他の誰かからthis postに出くわしました。私は、このような

from authentication.views import AccountViewSet, IndexView 

として私urls.pyにIndexViewのインポートを追加しました。そして、私はIndexViewエラーが離れて行って、サーバがエラーなしで走った私のviews.pyに

# authentication/views.py 
.... 
class IndexView(TemplateView): 
    template_name = 'mytestproject/index.html' #this page is showing when I visit http://localhost:8000/register rather than the one located at static/templates/authentication/register.html 

をIndexViewクラスを追加し、私が訪問したときhttp://localhost:8000/register何も表示されていませんでした。そのindex.htmlページを開いてコンテンツを追加し、そのコンテンツを表示しました。 Djangoは私が作成した登録ページではなく、authentication/templates/authentication.htmlにあるインデックスファイルを使用しています。 Djangoが登録URLを訪問したときにstatic/templates/authentication/register.htmlにあるテンプレートを使用するにはどうすればよいですか?私は混乱しています。なぜなら、「register」という名前のメソッドは、このチュートリアルではビューに定義されておらず、urls.pyファイルでも指定されていないからです。

おかげ'^.*$'

答えて

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

.*は、

あなたがurl.pyであなたの登録ページのURLを追加しdid'nt任意のURLは、このIndexViewに行くよう指示

url(r'^register/',views.yourview,name='givenameforthisurl') 
+0

これは私が混乱している部分です。チュートリアルのところでは、views.pyのサーバー側には「登録」メソッドは作成されていませんでした。そのURLをどのようなビューと名前にしていますか?私が作成したレジスタに関連する唯一のものは 'static/templates/authentication/register.html'にあるテンプレートです。チュートリアルにコンテンツがないか、わからないことがありますか?彼らはおそらく、そのテンプレートにアクセスするためにangleのルーティングに頼っていますか?結局のところ、それは静的フォルダに置かれました。 – user1852176

+0

はい、ページを表示するためにビュー関数を書く必要があります。あるいは、djangoの組み込みビューを使用している場合は、url.pyでこれらのパッケージをインポートするだけです(この例は組み込みの「認証ビュー」を参照してください)(https:// docs .djangoproject.com/ja/1.9/topics/auth/default /#module-django.contrib.auth.views) – masternone

関連する問題