PyCharm created using Django project and basic configuration

pycharm python is a very good development tools, greatly reducing the python project creation time and debugging time
after using python script to write for some time, want to try using Django to write a python project, now do the following recording notes:

1. Create a project

clipboard.png
If the local version is not installed corresponding to the selected version of Django python, pycharm automatically download the appropriate versions:

clipboard.png

After you create to run the project, the default page for the http://127.0.0.1:8000/ , open:

clipboard.png
Appears above the page, the project to create positive success

Directory Structure:

clipboard.png

2, create APP

In Django project may comprise a plurality of APP, corresponding to a large-scale project in the subsystem, sub-module, functional components and others, more independent from each other, but there are also links all the resources shared project APP

clipboard.png
Input: python manage.py startapp myapp
generate myapp folder

3, and the view configuration url

myapp / views.py file code:

from django.http import HttpResponse         #需要导入HttpResponse模块

def hello(request):                          #request参数必须有,名字类似self的默认规则,可以修改,它封装了用户请求的所有内容
    return HttpResponse("Hello world ! ")    #不能直接字符串,必须是由这个类封装,此为Django规则

testDjango / urls.py file code:

from myapp import views                      #首先需要导入对应APP的views

urlpatterns = [
    url(r'^admin/', admin.site.urls),        #admin后台路由
    url(r'^hello$', views.hello),            #你定义的路由,第一个参数为引号中的正则表达式,第二个参数业务逻辑函数(当前为views中的hello函数)
]

Run the project:
the command line is: python manage.py runserver 127.0.0.1:8000
but you can use the following methods in pycharm in:

clipboard.png

clipboard.png

clipboard.png

clipboard.png

4, Django template

Modify file views:

def hello(request):
    # return HttpResponse("Hello world ! ")
    context = {}
    context['hello'] = 'Hello World!'                #数据绑定
    return render(request, 'hello.html', context)    #将绑定的数据传入前台

Inherited template:

{#base.html#}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>模板测试</title>
</head>
<body>
    <h1>{{ hello }}</h1>
    <p>Django模板测试</p>
    {% block mainbody %}
       <p>original</p>
    {% endblock %}
</body>
</html>

hello.html inherited base.html, and replacement of particular block, the code hello.html modified as follows:

{#hello.html#}
{% extends "base.html" %}
 
{% block mainbody %}<p>继承了 base.html 文件</p>
{% endblock %}

Revisit address http://127.0.0.1:8000/hello , output results are as follows:

clipboard.png

5, the introduction of static files

Needs to be some static resources into the project, create a static directory, js, css and other files can be placed in this directory:

clipboard.png

Django need to find this directory, you need to configure the setting file:

clipboard.png

The introduction of static resources in the html file:

{% load staticfiles %}
{#base.html#}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{% static 'css/mypage.css' %}">
    <script src="{% static 'js/jquery-1.11.1.js' %}"></script>
<title>模板测试</title>
</head>
    <body>
        <h1>{{ hello }}</h1>
        <p>Django模板测试</p>
        {% block mainbody %}
           <p>original</p>
        {% endblock %}
    </body>
</html>

The first line of the file path is introduced static {% load staticfiles%}, was added in the <head> ... </ head> js li CSS files and the network chain

Guess you like

Origin www.cnblogs.com/sq1995liu/p/12103784.html