1 讓用戶輸入數(shù)據(jù)
1.1添加新主題:定義URL抛人,編寫視圖函數(shù)并編寫一個模板,導入包含表單form.py
1.用于添加主題的表單
使用ModelForm椭赋,創(chuàng)建forms.py稼病,存儲在models.py目錄
from?django?import?forms
from?.models?import?Topic
class?TopicForm(forms.ModelForm):
????class?Meta:
????????#?根據(jù)Topic創(chuàng)建表單
????????model?=?Topic
????????#?只包含字段text
????????fields?=?['text']
????????#?不為字段text生成標簽
????????labels?=?{'text':?''}
2.URL模式new_topic
添加到learning_logs/urls.py
????#將請求交給視圖函數(shù)new_topic
????path('new_topic/',views.new_topic,?name='new_topic'),
3.視圖函數(shù)new_topic()? ? 4.GET請求和POST請求
def?new_topic(request):
????'''添加新主題'''
????#?確定請求方式如果不是POST肮韧,返回空表單
????if?request.method?!=?'POST':
????????#?創(chuàng)建實例,空列表
????????form?=?TopicForm()
????else:
????????#?POST提交的數(shù)據(jù):對數(shù)據(jù)進行處理
????????#?request.POST存儲用戶輸入的數(shù)據(jù)
????????form?=?TopicForm(data=request.POST)
????????#?判斷字段是否有效
????????if?form.is_valid():
????????????form.save()
????????????#?保存后帮寻,使用redirect()將用戶瀏覽器重定向到頁面topics
????????????#?在頁面topics中將在主題列表中看到他剛輸入的主題
????????????return?redirect('learning_logs:topics')
? ? ? ? #?顯示空表單或指出表單數(shù)據(jù)無效
? ? ? ? context?=?{'form':?form}
? ? ? ? return?render(request,?'learning_logs/new_topic.html',?context)
5.模板new_topic 創(chuàng)建新模板new_topic.html
{%?extends?"learning_logs/base.html"?%}
{%?block?content?%}
????<p>Add?a?new?topic:</p>
? ? #定義一個表單乍狐,實參action告訴服務器將提交的表單數(shù)據(jù)發(fā)送到哪里
? ? #這里將它發(fā)回給視圖函數(shù)new_topic(),POST請求提交數(shù)據(jù)
????<form?action="{%?url?'learning_logs:new_topic'?%}"?method="post">
? ? ? ? #使用模板 來防止攻擊者利用表單獲得對服務器未經(jīng)授權的訪問(跨站請求偽造)
????????{%?csrf_token?%}
? ? ? ? #顯示表單as_p 以段落格式渲染所有表單元素
????????{{?form.as_p}}
? ? ? ? #提交按鈕
????????<button?name="submit">Add?topic</button>
????</form>
{%?endblock?content%}
6.鏈接到頁面new_topic
topics.html
<a?href="{%?url?'learning_logs:new_topic'?%}">Add?a?new?topic</a>
1.2添加新條目
1.用于添加新條目的表單
forms.py
from?.models?import?Topic,?Entry
~~~~~~
class?EntryForm(forms.ModelForm):
????class?Meta:
????????model?=?Entry
????????fields?=?['text']
????????labels?=?{'text':?''}
????????#小部件widget是HTML表單元素,Textarea定制字段text輸入小部件固逗,寬度設置80列
????????widgets?=?{'text':?forms.Textarea(attrs={'cols':?80})}
2.URL模式new_entry
path('new_entry/<int:topic_id>/',?views.new_entry,?name='new_entry'),
匹配時浅蚪,Django將請求和主題ID發(fā)送給函數(shù)new_entry()
3.視圖函數(shù)views.py
from?.forms?import?TopicForm,?EntryForm
def?new_entity(request,?topic_id):
????'''在特定主題中添加新條目'''
????topic?=?Topic.objects.get(id=topic_id)
????if?request.method?!=?'POST':
????????form?=?EntryForm()
????else:
????????form?=?EntryForm(data=request.POST)
????????if?form.is_valid():
????????????new_entry?=?form.save(commit=False)
????????????new_entry.topic?=?topic
????????????new_entry.save()
????????????return?redirect('learning_logs:topic',?topic_id=topic_id)
????#?顯示空表單或指出表單數(shù)據(jù)無效
????context?=?{'topic':?topic,?'form':?form}
????return?render(request,?'learning_logs/new_entry.html',?context)
4.模板new_entry
{%?extends?"learning_logs/base.html"?%}
{%?block?content?%}
????<p><a?href="{%?url?'learning_logs:topic'?topic.id?%}">{{?topic?}}</a></p>
????<p>Add?a?new?entry:</p>
????<form?action="{%?url?'learning_logs:new_entry'?topic.id?%}"?method="post">
????????{%?csrf_token?%}
????????{{?form.as_p}}
????????<button?name="submit">Add?entry</button>
????</form>
{%?endblock?content?%}
5.鏈接到頁面new_entry
topic.html
????<p>Topic:{{?topic?}}</p>
????<p>Entries:</p>
????<p>
????????<a?href="{%?url?'learning_logs:new_entry'?topic.id?%}">Add?new?entry</a>
????</p>
1.3編輯條目
1.URL模式edit_entry
urls.py
????path('edit_entry/<int:entry_id>/',?views.edit_entry,?name='edit_entry'),
2.視圖函數(shù)edit_entry()
views.py
from?.models?import?Topic,?Entry??#?導入所需數(shù)據(jù)相關聯(lián)的模型
def?edit_entry(request,?entry_id):
????'''編輯現(xiàn)有條目'''
????entry?=?Entry.objects.get(id=entry_id)
????topic?=?entry.topic
????if?request.method?!=?'POST':
????????form?=?EntryForm(instance=entry)
????else:
????????form?=?EntryForm(instance=entry,?data=request.POST)
????????if?form.is_valid():
????????????form.save()
????????????return?redirect('learning_logs:topic',?topic_id=topic_id)
????context?=?{'entry':?entry,?'topic':?topic,?'form':?form}
????return?render(request,?'learning_logs/edit_entry.html',?context)
3.模板edit_entry
{%?extends?"learning_logs/base.html"?%}
{%?block?content?%}
????<p><a?href="{%?url?'learning_logs:topic'?topic.id?%}">{{?topic?}}</a></p>
????<p>Edit?entry:</p>
????<form?action="{%?url?'learning_logs:edit_entry'?topic.id?%}"?method="post">
????????{%?csrf_token?%}
????????{{?form.as_p}}
????????<button?name="submit">Save?changes</button>
????</form>
{%?endblock?content?%}
4.鏈接到頁面edit_entry
topic.html
<p>{{?entry.date_added|date:'M?d,?Y?H:i'?}}</p>
????????????<p>{{?entry.text|linebreaks?}}</p>
????????????<p>
????????????????<a?href="{%?url?'learning_logs:edit_entry'?entry.id?%}">Edit?entry</a>
????????????</p>
放在日期和文本后面