THUMBS SHIFT→

このブログは主に親指シフトを用いて書かれています

django チュートリアル その1

確認したらdjangoバージョンが古かったのでpipでアップデートできないかなと
色々いじってみたけど俺の知識じゃ綺麗に解決できなかったので公式ドキュメントかどっかにあった再インストールする方法でアップデートした

$ pip uninstall django
$ pip install django

今思ったけど

pip install django -U

で行けたかもしれん。あとmezzanineはdjango2.0をサポートしてるのかどうか不安。

djangは2.0になってurl関数がpath関数に置き換わったようで

url(r'^(?P<変数名>正規表現)', ......)

とやっていたのが

path('<変数型:変数名>')

になったらしい。正規表現でできたような文字数指定とかはできないっぽい?よくわからん。re_path関数がdjango1.xでのurl関数と同じ働きをするみたいなので、詳細にurlを指定したかったらこっちを使えってことかな?。
変数型で使えるのが以下

Path converters

The following path converters are available by default:

str - Matches any non-empty string, excluding the path separator, '/'. This is the default if a converter isn't included in the expression.
int - Matches zero or any positive integer. Returns an int.
slug - Matches any slug string consisting of ASCII letters or numbers, plus the hyphen and underscore characters. For example, building-your-1st-django-site.
uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.
path - Matches any non-empty string, including the path separator, '/'. This allows you to match against a complete URL path rather than just a segment of a URL path as with str.

簡単に翻訳すると

str - 空でないすべての文字列にマッチ。ただし「 / 」を除く。
int - 0 か自然数にマッチ。
slug - ASCIIの文字と数字, ハイフン, アンダースコアからなる文字列にマッチ。例:building-your-1st-django-site
uuid - uuidにマッチする。複数のURLが同じページを指定しないようにdashを含み、小文字でなければならない。*1
例:075194d3-6885-417e-a8a8-6c931e272f00
UUIDインスタンスを返す。
path - 「/」を含んだ空でない文字列にマッチする。strを使った区切りがあるURL指定ではなく完全に自分でURLを指定できる。

strとslugの違いがわかんね。slugのほうが制約がきつい?まぁとりあえずstrとint使っとけばいいと思います。あと自分でintとかslugみたいな変数型(path converterというらしい)を作ることもできるっぽい。詳しくは以下
参照

URL ディスパッチャ | Django documentation | Django

*1:dashはハイフンに似た記号らしい