python basis Tutorial: Create a Django project PyCharm and basic configuration in detail

pycharm python is a very good development tools, greatly reducing the time to create a python project and debugging time. This article describes the use of PyCharm create a Django project and basic configuration in detail, pycharm is a very good python development tools, greatly reducing the time of creation and debugging time python project
pycharm is a very good python 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 Here Insert Picture Description
if not installed locally corresponds to the selected version of Django python version, pycharm will automatically download the appropriate version: Here Insert Picture Description
Create a project run, the default page for the http://127.0.0.1:8000/, after opening: Here Insert Picture Description
appears above page, the project to create positive success

Directory structure: Here Insert Picture Description
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 Here Insert Picture Description
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规则

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: Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
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>

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: Here Insert Picture Description
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: Here Insert Picture Description
need to Django find this directory, you need to configure the setting file: Here Insert Picture Description
Import a static resource in html file:

{% load staticfiles %}
{#base.html#}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="{% static 'css/mypage.css' %}" rel="external nofollow" >
  <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 introduction of a static file path {% load staticfiles%}, added ... in the network chain CSS and js files
last to recommend a very wide python learning resource gathering, [click to enter] , here are my collection before learning experience , study notes, there is a chance of business experience, and calmed down to zero on the basis of the actual project data, we can at the bottom, leave a message, do not know to put forward, we will study together progress

Published 22 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/haoxun08/article/details/104740633