ディレクトリ
Pythonの研究ノート--Day10
昨日に移動します。
登録申請
昨日は、今日、我々は、このアプリケーションで何かをしなければならない、アプリケーションが登録されています。そのような「ハローワールド」など、簡単なページを書き込みます。
次のディレクトリのアプリケーションを変更します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>")
アプリケーションディレクトリにして、urls.py URLのための新しいマップを作成します。
from django.urls import path
from a_test import views
urlpatterns [
path('', views.index, name='index')
]
変更されたプロジェクトの下のurls.pyファイルには、私たちは、アプリケーションで構成されたURLをマージ:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('a_test/', include('a_test.urls')),
]
私たちは()メソッドは、合併によって送信されますが、プロジェクトのURL URLの設定ファイルにアプリケーション構成ファイルを含めると「a_test /」パスにマッピングされ、完全なthese're行われ、その後、我々は唯一のプロジェクトを実行する必要があります。
python manage.py runserver
ブラウザのアドレスバーに入力しhttp://localhost:8000/a_test/
、私たちだけの書き込み「こんにちは世界」へのアクセスに。
ビュー
我々はまた、ビューテンプレートを通じてsettings.pyのコンフィギュレーションテンプレートパスをプロジェクトディレクトリにフォルダを作成するためのテンプレートを、Webページを表示することができます。
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',
],
},
},
]
テンプレートのページを作成します。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>
{{メッセージ}}プレースホルダテンプレート構文、Djangoテンプレート言語の一部。Views.pyは、下のアプリケーションディレクトリを変更し続けます。
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!!!'})
私たちのプロジェクトを再実行します。
python manage.py runserver
リフレッシュした後、hello world!!!
それが現れました。
デザインモデル
ストアデータ、文字テーブル、書籍のテーブルに2つのテーブルを設計します。
- ブックテーブルの構造設計:
- テーブル名:書籍
- 題名:名前
- 発行:pub_time
- 文字テーブルの構造設計:
- 表名:著者
- 著者:著者
- 性別:性別に
- 作品:ブック
構造帳の文字対多の関係
settings.pyでは、データベース、DjangoのデフォルトのSQLiteデータベースによってデータベースを構成することができます
次に、私たちは、クラスファイルの定義がa_testにmodels.pyのモデルを適用します
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
にviews.pyにデータを格納する方法を呼び出すためのインタフェースを作成します。
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")
内のコンフィギュレーションurls.pyにURLを追加します。
from django.urls import path
from a_test import views
urlpatterns = [
path('', views.index, name='index'),
path('make/', views.makeData, name='make'),
]
して、ブラウザアクセスhttp://localhost:8000/a_test/make
されるデータベースにデータを保存した後、私たちはインデックスメソッドのviews.pyを変更します。
def index(request):
people = People.objects.all()
return render(request, 'index.html', {'people': people})
テンプレートはindex.htmlのファイルをテンプレートに変更します。
<!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>
最後の訪問は、http://localhost:8000/a_test/
ページに表示されるデータを見ることができます。だから、私たちの簡単なエントリは完了です。
エピローグ
それだけの簡単なデモが、しかし、Djangoフレームワーク、明日、土曜日、学ぶために継続するための一般的な意識があり、ちょうど非常にシンプルなエントリーです!
エラーがある場合、私の記事を見つけるか、何かいいアイデアは、私に連絡することができている場合は、私たちが一緒に進行一緒に勉強し、私のメールアドレスは[email protected]です
のは、これらの複数の操作を行いましょう!