每次使用 Django 时如何设置? (即使用 **source bin/activate** 和 ** python manage.py runserver**)
How to setup Django every time you use it? (i.e. using **source bin/activate** and ** python manage.py runserver**)
我是 Django 的初学者,对于每次打开新终端时如何始终如一地设置 Django 感到很困惑。每当我这样做时,我必须始终重新启动并执行以下操作:
将目录 (cd
) 更改为我的 trydjango 目录,我在其中启动虚拟环境 (source bin/activate
)
终端有最左边的自动(trydjango)
后(trydjango) lyons-MacBook-Air:trydjango lyons$
就知道我开启了虚拟环境
然后,我通过输入python manage.py runserver
的终端进入src目录
每当我进入 runserver
步骤(第 3 步)时,我都会 运行 陷入一致的错误(例如 AttributeError)。我做错了什么吗?
以下是错误的完整Traceback
(trydjango) lyons-MacBook-Air:src lyons$ python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d5a6b90>
Traceback (most recent call last):
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/lyons/Dev/trydjango/src/trydjango/urls.py", line 22, in <module>
path('', home_view, name='home'),
NameError: name 'home_view' is not defined
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d4a3b90>
Traceback (most recent call last):
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/lyons/Dev/trydjango/src/trydjango/urls.py", line 22, in <module>
path('', views.home_view, name='home'),
AttributeError: module 'pages.views' has no attribute 'home_view'
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home_view(*args, **kwargs): # args, kwargs
return HttpResponse("<h1>Hello World</h1>") # string of HTML code
urls.py
"""trydjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from pages import views #from pages import views
urlpatterns = [
path('', views.home_view, name='home'),
path('admin/', admin.site.urls),
]
要告诉您启动项目的具体步骤并不容易。
django-admin startproject new_project_name
创建一个新项目
manage.py runserver IP:Port
运行 服务器启动时,http://127.0.0.1:8000/, or http://localhost:8000/
manage.py startapp new_APP_name
在new_project_name创建一个新APP
manage.py makemigrations
在new_project_name告诉django项目改变了,然后建立一个迁移文件。
manage.py migrate
在 new_project_name 更新。
在new_project_name/new_project_name/settings.py
INSTALLED_APPS
在 new_APP_name\apps.py
中添加带有 new_APP_name.apps.new Appconfig subclass name
的项目
在new_project_name/new_project_name/urls.py中添加你URL解析器,例如
from . import views
urlpatterns = [
path('', views.Topic, name='index')
]
以new_project_name/new_APP_name/admin.py寄存器模型为例,
from .models import Topic
admin.site.register(Topic)
我是 Django 的初学者,对于每次打开新终端时如何始终如一地设置 Django 感到很困惑。每当我这样做时,我必须始终重新启动并执行以下操作:
将目录 (
cd
) 更改为我的 trydjango 目录,我在其中启动虚拟环境 (source bin/activate
)终端有最左边的自动
(trydjango)
后(trydjango) lyons-MacBook-Air:trydjango lyons$
就知道我开启了虚拟环境然后,我通过输入
的终端进入src目录python manage.py runserver
每当我进入 runserver
步骤(第 3 步)时,我都会 运行 陷入一致的错误(例如 AttributeError)。我做错了什么吗?
以下是错误的完整Traceback
(trydjango) lyons-MacBook-Air:src lyons$ python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d5a6b90>
Traceback (most recent call last):
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/lyons/Dev/trydjango/src/trydjango/urls.py", line 22, in <module>
path('', home_view, name='home'),
NameError: name 'home_view' is not defined
Performing system checks...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d4a3b90>
Traceback (most recent call last):
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
self.check(display_num_errors=True)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/management/base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
for pattern in self.url_patterns:
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 540, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/utils/functional.py", line 36, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/lyons/Dev/trydjango/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/lyons/Dev/trydjango/src/trydjango/urls.py", line 22, in <module>
path('', views.home_view, name='home'),
AttributeError: module 'pages.views' has no attribute 'home_view'
views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home_view(*args, **kwargs): # args, kwargs
return HttpResponse("<h1>Hello World</h1>") # string of HTML code
urls.py
"""trydjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from pages import views #from pages import views
urlpatterns = [
path('', views.home_view, name='home'),
path('admin/', admin.site.urls),
]
要告诉您启动项目的具体步骤并不容易。
django-admin startproject new_project_name
创建一个新项目manage.py runserver IP:Port
运行 服务器启动时,http://127.0.0.1:8000/, or http://localhost:8000/manage.py startapp new_APP_name
在new_project_name创建一个新APPmanage.py makemigrations
在new_project_name告诉django项目改变了,然后建立一个迁移文件。manage.py migrate
在 new_project_name 更新。在new_project_name/new_project_name/settings.py
INSTALLED_APPS
在 new_APP_name\apps.py 中添加带有 在new_project_name/new_project_name/urls.py中添加你URL解析器,例如
from . import views urlpatterns = [ path('', views.Topic, name='index') ]
以new_project_name/new_APP_name/admin.py寄存器模型为例,
from .models import Topic admin.site.register(Topic)
new_APP_name.apps.new Appconfig subclass name
的项目