The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r’^$’, views.home, name=’home’) Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r’^$’, Home.as_view(), name=’home’) Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r’^blog/’, include(‘blog.urls’)) “”” from django.conf.urls import include,url
from django.contrib import admin
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations:
Applying contenttypes.0001_initial… OK
Applying auth.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying sessions.0001_initial… OK
「【さくらVPS】【Python】Django で Web アプリを作る(Webアプリ構築編)【Part.7】」でも解説しましたが、「python manage.py migrate」コマンドについて再度解説します。
(pyenv) [test@SAKURA_VPS message_app]$ pwd /home/test/pyenv/message_site/message_app/templates/message_app
(pyenv) [test@SAKURA_VPS message_app]$ vi result.html {% if result_list %} {% for i in result_list %} <p>{{ i.new_message }}</p>
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py makemigrations message_app Migrations for ‘message_app’: message_app/migrations/0001_initial.py
– Create model Message_bord
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py migrate Operations to perform: Apply all migrations: admin, auth, contenttypes, message_app, sessions Running migrations:
Applying message_app.0001_initial… OK
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py runserver
Performing system checks…
System check identified no issues (0 silenced).
November 06, 2017 – 21:31:11
Django version 1.11.7, using settings ‘message_site.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py makemigrations message_app
Traceback (most recent call last):
File “manage.py”, line 22, in
execute_from_command_line(sys.argv)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/__init__.py”, line 364, in execute_from_command_line
utility.execute()
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/__init__.py”, line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 283, in run_from_argv
self.execute(*args, **cmd_options)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 327, in execute
self.check()
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 359, in check
include_deployment_checks=include_deployment_checks,
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 346, in _run_checks
return checks.run_checks(**kwargs)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/checks/registry.py”, line 81, in run_checks
new_errors = check(app_configs=app_configs)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/checks/urls.py”, line 16, in check_url_config
return check_resolver(resolver)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/checks/urls.py”, line 26, in check_resolver
return check_method()
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/resolvers.py”, line 254, in check
for pattern in self.url_patterns:
File “/home/test/pyenv/lib64/python3.6/site-packages/django/utils/functional.py”, line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/resolvers.py”, line 405, in url_patterns
patterns = getattr(self.urlconf_module, “urlpatterns”, self.urlconf_module)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/utils/functional.py”, line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/resolvers.py”, line 398, in urlconf_module
return import_module(self.urlconf_name)
File “/usr/lib64/python3.6/importlib/__init__.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “<frozen importlib._bootstrap>”, line 994, in _gcd_import
File “<frozen importlib._bootstrap>”, line 971, in _find_and_load
File “<frozen importlib._bootstrap>”, line 955, in _find_and_load_unlocked
File “<frozen importlib._bootstrap>”, line 665, in _load_unlocked
File “<frozen importlib._bootstrap_external>”, line 678, in exec_module
File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed
File “/home/test/pyenv/message_site/message_site/urls.py”, line 21, in
url(r’^message_app/’, include(‘message_app.urls’)),
File “/home/test/pyenv/lib64/python3.6/site-packages/django/conf/urls/__init__.py”, line 50, in include
urlconf_module = import_module(urlconf_module)
File “/usr/lib64/python3.6/importlib/__init__.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “<frozen importlib._bootstrap>”, line 994, in _gcd_import
File “<frozen importlib._bootstrap>”, line 971, in _find_and_load
File “<frozen importlib._bootstrap>”, line 955, in _find_and_load_unlocked
File “<frozen importlib._bootstrap>”, line 665, in _load_unlocked
File “<frozen importlib._bootstrap_external>”, line 678, in exec_module
File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed
File “/home/test/pyenv/message_site/message_app/urls.py”, line 3, in
urlpattern [ NameError: name ‘urlpattern’ is not defined
(pyenv) [test@SAKURA_VPS message_site]$
例えば、上記ログですが「NameError: name ‘urlpattern’ is not defined」と分かりやすくメッセージが表示されています。
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py makemigrations message_app
Traceback (most recent call last):
File “manage.py”, line 22, in
execute_from_command_line(sys.argv)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/__init__.py”, line 364, in execute_from_command_line
utility.execute()
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/__init__.py”, line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 283, in run_from_argv
self.execute(*args, **cmd_options)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 327, in execute
self.check()
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 359, in check
include_deployment_checks=include_deployment_checks,
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/management/base.py”, line 346, in _run_checks
return checks.run_checks(**kwargs)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/checks/registry.py”, line 81, in run_checks
new_errors = check(app_configs=app_configs)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/checks/urls.py”, line 16, in check_url_config
return check_resolver(resolver)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/checks/urls.py”, line 26, in check_resolver
return check_method()
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/resolvers.py”, line 254, in check
for pattern in self.url_patterns:
File “/home/test/pyenv/lib64/python3.6/site-packages/django/utils/functional.py”, line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/resolvers.py”, line 405, in url_patterns
patterns = getattr(self.urlconf_module, “urlpatterns”, self.urlconf_module)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/utils/functional.py”, line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/resolvers.py”, line 398, in urlconf_module
return import_module(self.urlconf_name)
File “/usr/lib64/python3.6/importlib/__init__.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “<frozen importlib._bootstrap>”, line 994, in _gcd_import
File “<frozen importlib._bootstrap>”, line 971, in _find_and_load
File “<frozen importlib._bootstrap>”, line 955, in _find_and_load_unlocked
File “<frozen importlib._bootstrap>”, line 665, in _load_unlocked
File “<frozen importlib._bootstrap_external>”, line 678, in exec_module
File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed
File “/home/test/pyenv/message_site/message_site/urls.py”, line 21, in
url(r’^message_app/’, include(‘message_app.urls’)),
File “/home/test/pyenv/lib64/python3.6/site-packages/django/conf/urls/__init__.py”, line 50, in include
urlconf_module = import_module(urlconf_module)
File “/usr/lib64/python3.6/importlib/__init__.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “<frozen importlib._bootstrap>”, line 994, in _gcd_import
File “<frozen importlib._bootstrap>”, line 971, in _find_and_load
File “<frozen importlib._bootstrap>”, line 955, in _find_and_load_unlocked
File “<frozen importlib._bootstrap>”, line 665, in _load_unlocked
File “<frozen importlib._bootstrap_external>”, line 678, in exec_module
File “<frozen importlib._bootstrap>”, line 219, in _call_with_frames_removed File “/home/test/pyenv/message_site/message_app/urls.py”, line 4, in url(r’^$’, views.index, name=’index’), AttributeError: module ‘message_app.views’ has no attribute ‘index’
(pyenv) [test@SAKURA_VPS message_site]$
(pyenv) [test@SAKURA_VPS message_site]$ python manage.py runserver
Performing system checks…
System check identified no issues (0 silenced).
November 06, 2017 – 21:31:11
Django version 1.11.7, using settings ‘message_site.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Not Found: /
[06/Nov/2017 21:31:57] “GET / HTTP/1.1” 404 2030
[06/Nov/2017 21:32:10] “GET /message_app HTTP/1.1” 301 0 Internal Server Error: /message_app/
Traceback (most recent call last):
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/base.py”, line 77, in reverse
extra, resolver = resolver.namespace_dict[ns] KeyError: ‘message_app’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/handlers/exception.py”, line 41, in inner
response = get_response(request)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/handlers/base.py”, line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/core/handlers/base.py”, line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File “/home/test/pyenv/message_site/message_app/views.py”, line 18, in message_app_index
return render(request, ‘message_app/index.html’, contexts)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/shortcuts.py”, line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/loader.py”, line 68, in render_to_string
return template.render(context, request)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/backends/django.py”, line 66, in render
return self.template.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 207, in render
return self._render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 199, in _render
return self.nodelist.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 990, in render
bit = node.render_annotated(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 957, in render_annotated
return self.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/loader_tags.py”, line 177, in render
return compiled_parent._render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 199, in _render
return self.nodelist.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 990, in render
bit = node.render_annotated(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 957, in render_annotated
return self.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/loader_tags.py”, line 72, in render
result = block.nodelist.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 990, in render
bit = node.render_annotated(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/base.py”, line 957, in render_annotated
return self.render(context)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/template/defaulttags.py”, line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File “/home/test/pyenv/lib64/python3.6/site-packages/django/urls/base.py”, line 87, in reverse raise NoReverseMatch(“%s is not a registered namespace” % key) django.urls.exceptions.NoReverseMatch: ‘message_app’ is not a registered namespace
[06/Nov/2017 21:32:11] “GET /message_app/ HTTP/1.1” 500 146280
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r’^$’, views.home, name=’home’)
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r’^$’, Home.as_view(), name=’home’)
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r’^blog/’, include(‘blog.urls’))
“””
from django.conf.urls import include,url
from django.contrib import admin
コメント