Django learning---creation, application of Day1-app and environment configuration in Pycharm

content

  • Configuration of virtual environment in pycharm
  • Configure Debug in pycharm
  • Create your first application (app)
  • Defining methods in views in myapp application
  • Configuration of urls under myproject project
  • Configuration of urls under myapp project

Configuration of virtual environment in pycharm

1. According to the content of the previous section, move its content to the pycharm artifact to continue the operation, but before the operation, you need to configure the environment in pycharm.
Under Settings -> Project -> Project interpretor, put the previously created with The virtual environment path of python.exe is loaded here as the python environment and virtualenv virtual environment that continues to run on pycharm

Configure Debug in pycharm

Debug is used to debug code
1. Configure the python interpreter under Debug under the Run menu bar in pycharm
2. Configure Name, Scripts path and Parameters respectively
Name:
Scripts path:
Parameters:

Create your first application (app)

1. Create an application named under the myproject project
Format : python manage.py startapp app-name
Here, the application (app) is created directly in pycharm, which is the same as the command in cmd. The example of
python manage.py startapp myapp
is as follows :
The contents of the application (app) include the following:
a init .py : initialization
b admin.py : management of the background registration model
c apps.py : needed when registering an app in the settings environment. But it is not recommended to use
d models.py here: the place to write the model
e tests.py:
f urls.py
g views.py: the place to write the business logic
Emphasis : the most used here are d, f and g

Defining methods in views in myapp application

When using HttpResponse here, you need to import the following modules
from django.http import HttpResponse

Define a method for hello_girl

def hello_girl(request):

# 返回一个Hello, beautiful girl的页面对象
return HttpResponse('Hello, beautiful girl')

Configuration of urls under myproject project

1. Configure urlpatterns
from django.conf.urls import url, include
from django.contrib import admin
from myapp import views under the urls file in myproject project

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^myapp/', include(myapp.urls)),
# Here hello_girl is the path, through which you can access the hello_girl method In the Hello, beautiful girl object, pay attention to the / behind hello/, which represents the matching
url of the path (r'^hello_girl/', views.hello_girl),

Configuration of urls under myapp project

2. Create a new urls folder under the myapp project, and configure the urls in
this directory just to match the urls path under the myproject project
from django.conf.urls import url
from myapp import views

urlpatterns = [
url(r'^hello_girl/', views.hello_girl),
]
Visit the page address as follows:
127.0.0.1:8000/myapp/hello_girl/
and it will display: Hello, beautiful girl

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325781504&siteId=291194637