LoginSignup
6
4

More than 5 years have passed since last update.

ルーティングでこける Udemy 【3日でできる】はじめての Django 入門

Last updated at Posted at 2018-12-30

はじめに

レクチャー21でルーティングの設定をするのですが、私が講座と違うバージョンのDjango(2系)を使っているためちょっとハマりました。

これから【3日でできる】はじめての Django 入門 ( Python 3 でウェブアプリを作って AWS EC2 で公開!)を学ぶ方の中にも僕と同じ進め方をする方もいると思うので、共有します。

起こったこと

http://127.0.0.1:8000/posts/」にアクセスするも404エラーとなる。
ログに警告

WARNINGS:
?: (2_0.W001) Your URL pattern '^$' [name='index'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().

が吐かれていたので、多分正規表現が混ざる記述がまずかったのだろう。。。

僕の場合、諸悪の根源がこいつ↓でした

urls.py(posts配下)
from django.urls import path

from . import views

urlpatterns = [
    path(r'^$',views.index, name='index')
      ↑
     「Regular Expressions」=正規表現で使う「r」
]

正しくは(理解しきれていないので、正確に表現するならば「動作させるには」というべきかもしれない)

urls.py(posts配下)
from django.urls import path

from . import views

urlpatterns = [
    path('',views.index, name='index')
]

その他のファイル状況は基本的に動画と同じ
urlではなくpathを用いているものとして

urls.py(myblogapp配下)
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls) ,
    path('posts/', include('posts.urls') )
]

も掲載しておきます。

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4