table of Contents
Python study notes --Day10
Moving on yesterday.
Registration Application
Yesterday has registered an application, today we have to do something with this application. To write a simple page, such as "hello world".
Modify the application of the following directories views.py
:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h3>hello world!<h3>")
In the application directory and then create a new map for urls.py URL:
from django.urls import path
from a_test import views
urlpatterns [
path('', views.index, name='index')
]
urls.py file under the modified project, we just merge URL configured in the application:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('a_test/', include('a_test.urls')),
]
We () method will be sent by the merger include application configuration file to the project url url configuration file and mapped to 'a_test /' path, complete these're done, then we only need to run the project:
python manage.py runserver
Enter the browser address bar http://localhost:8000/a_test/
to access to our just write "hello world".
view
We can also display a web page through the view template, templates to create a folder in the project directory, configuration template path in settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # 添加这里
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Create a template page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
{{Message}} placeholder template syntax, part of Django template language. Views.py continue to modify the application directory under:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return render(request, 'index.html', {'message': 'hello world!!!'})
Re-run our project:
python manage.py runserver
After refreshing, hello world!!!
it appeared.
Design Model
Design two tables to store data, a character table, a table of books.
- Book table structure design:
- Table Name: book
- Title: name
- Published: pub_time
- Character table structure design:
- Table name: author
- Author: author
- On gender: gender
- Works: book
structure book characters-many relationship
In settings.py we can configure a database by DATABASES, Django default SQLite database
Next, we apply the model class file defines models.py in a_test
from django.db import models
# Create your models here.
class Book(models.Model):
name = models.CharField(max_length=32)
pub_time = models.DateTimeField()
def __str_(self):
return "%d" % self.pk
class People(models.Model):
name = models.CharField(max_length=32)
gender = models.BooleanField()
book = models.ForeignKey('Book',on_delete=models.CASCADE)
def __str_(self):
return "%d" % self.pk
Create an interface to invoke a method of storing data in views.py in:
def makeData(request):
book = Book()
book.name = "射雕英雄传"
book.pub_date=datetime(year=1990,month=1,day=10)
book.save()
# 人物
people = People()
people.name = "郭靖"
people.gender = True
people.book = book
people.save()
people = People()
people.name = "黄蓉"
people.gender = False
people.book = book
people.save()
# 返回
result = [book, people]
return HttpResponse("success")
Add a url in configuration urls.py in:
from django.urls import path
from a_test import views
urlpatterns = [
path('', views.index, name='index'),
path('make/', views.makeData, name='make'),
]
Browser access by http://localhost:8000/a_test/make
saving the data to the database will be, and then we modify the index method views.py in:
def index(request):
people = People.objects.all()
return render(request, 'index.html', {'people': people})
Modify templates template index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>人物列表</h1>
<ul>
{%for p in people%}
<li>
{{ p.name }},
性别:
{%if p.gender == True%}
男
{% else %}
女
{% endif %},
所属图书:{{ p.book.name }},
图书发布时间:{{ p.book.pub_time }}
</li>
{%endfor%}
</ul>
</body>
</html>
Last Visitors http://localhost:8000/a_test/
can see the data displayed on the page. So that our simple entry is complete.
Epilogue
It's just a very simple entry, although only a simple demo, but there is a general awareness of the Django framework, tomorrow, Saturday, to continue to learn!
If you find my articles where there is an error or have any good ideas can contact me, we study together progress together, my email address is [email protected]
let’s do more of those!