Part 3 - Advanced Concepts(11-13)
https://simpleisbetterthancomplex.com/series/2017/09/18/a-complete-beginners-guide-to-django-part-3.html
https://github.com/sibtc/django-beginners-guide/tree/v0.3-lw
from django.conf.urls import url
from django.contrib import admin
from boards import views urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^boards/(?P<pk>\d+)/$', views.board_topics, name='board_topics'),
url(r'^boards/(?P<pk>\d+)/new/$', views.new_topic, name='new_topic'),
url(r'^admin/', admin.site.urls),
]
python manage.py makemigrations
python manage.py sqlmigrate boards 0001
python manage.py migrate python manage.py shell from boards.models import Board
board = Board.objects.create(name='Django', description='This is a board about Django.')
board = Board.objects.create(name='Python', description='General discussion about Python.')
board = Board.objects.create(name='Random', description='Here you can discuss about anything.') boards_list = Board.objects.all()
for board in boards_list:
print(board.description)
from django.contrib.auth.models import User
from django.shortcuts import render, redirect, get_object_or_404
from .models import Board, Topic, Post def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk) if request.method == 'POST':
subject = request.POST['subject']
message = request.POST['message']
user = User.objects.first() # TODO: 临时使用一个账号作为登录用户 topic = Topic.objects.create(
subject=subject,
board=board,
starter=user
) post = Post.objects.create(
message=message,
topic=topic,
created_by=user
) return redirect('board_topics', pk=board.pk) # TODO:redirect to the created topic page
return render(request, 'new_topic.html', {'board': board})
#boards/forms.py
from django import forms
from .models import Topic class NewTopicForm(forms.ModelForm):
message = forms.CharField(widget=forms.Textarea(), max_length=4000) class Meta:
model = Topic
fields = ['subject', 'message']
#boards/views.py
from django.contrib.auth.models import User
from django.shortcuts import render, redirect, get_object_or_404
from .forms import NewTopicForm
from .models import Board, Topic, Post def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk)
user = User.objects.first() # TODO: get the currently logged in user
if request.method == 'POST':
form = NewTopicForm(request.POST)
if form.is_valid(): topic = form.save(commit=False)
topic.board = board
topic.starter = user
topic.save() post = Post.objects.create(
message=form.cleaned_data.get('message'),
topic=topic,
created_by=user
)
return redirect('board_topics', pk=board.pk) # TODO: redirect to the created topic page
else:
form = NewTopicForm()
return render(request, 'new_topic.html', {'board': board,'form': form})
#这是我们在 view(视图) 中处理 form(表单) 的方式。
#让我们去掉一些多余的部分,只看表单处理的核心部分:
if request.method == 'POST':
form = NewTopicForm(request.POST)
if form.is_valid():
topic = form.save()
return redirect('board_topics', pk=board.pk)
else:
form = NewTopicForm()
return render(request, 'new_topic.html', {'form': form})
myproject/
|-- myproject/
| |-- boards/
| | |-- admin.py
| | |-- forms.py
| | |-- models.py
| | +-- views.py
| |-- myproject/
| | |-- settings.py
| | +-- urls.py
| |-- static/
| | |-- css/
| | | |-- app.css
| | | +-- bootstrap.min.css
| |-- templates/
| | |-- includes/
| | | +-- forms.html
| | |-- base.html
| | |-- home.html
| | |-- topic.html
| | +-- new_topics.html
Part 3 - Advanced Concepts(11-13)的更多相关文章
- Notes of Daily Scrum Meeting(11.13)
Notes of Daily Scrum Meeting(11.13) 今天邹欣老师给我们讲课大家还是很有收获的,大家课堂的参与度确实有了很大的提升,而且邹欣老师关于项目Scrum Meeting报告 ...
- The 10th Shandong Provincial Collegiate Programming Contest(11/13)
$$The\ 10th\ Shandong\ Provincial\ Collegiate\ Programming\ Contest$$ \(A.Calandar\) 签到 //#pragma co ...
- (11.13)Java小知识!
今天想要与大家分享一下有关于构造方法的知识! 构造方法的定义与作用 构造方法是一种特殊类型的方法.当一个对象被创建的时候,构造方法用来初始化对象,也就是说构造方法其实是一个名词而不是动词,像我刚刚开始 ...
- 2017-2018 ACM-ICPC Latin American Regional Programming Contest PART (11/13)
$$2017-2018\ ACM-ICPC\ Latin\ American\ Regional\ Programming\ Contest$$ \(A.Arranging\ tiles\) \(B. ...
- VR/AR软件—Mirra测试(截至2017/11/13),使AR/VR创作更加便捷
Mirra(截至2017/11/13)https://www.mirra.co/ 1.主要特点: 目前仅支持VR,不支持AR 在浏览器(仅支持chrome,firefox)上进行创作,但目前不能直接在 ...
- (Problem 13)Large sum
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072875339 ...
- (1.13)mysql优化数据库对象
(1.13)mysql优化数据库对象 1.mysql优化数据库对象 [1.1]数据库对象类型优化 select * from test1 procedure analyse(); ,); --不要为那 ...
- (3.13)mysql基础深入——mysql日志分析工具之mysqlsla【待完善】
(3.13)mysql基础深入——mysql 日志分析工具之mysqlsla 关键字:Mysql日志分析工具.mysqlsla 常用工具 [1]mysqldumpslow:官方提供的慢查询日志分析工具 ...
- Notes of Daily Scrum Meeting(11.19)
Notes of Daily Scrum Meeting(11.19) 现在工程项目进入尾声了,我们的项目中还有一些问题需要解决,调试修改起来进度比较慢,所以昨天就没有贴出项目 进度,今天的团队工作总 ...
- Notes of Daily Scrum Meeting(11.17)
Notes of Daily Scrum Meeting(11.17) 今天是第四周的周一,也就是说距离最后发布也只剩下一周的时间,但我们的工程里面还有很多的问题没有解决,我关注过 其他一两个小组,他 ...
随机推荐
- delphi IDE RAD 丢失的快捷键 Ctrl+Shift+F
丢失的Ctrl+Shift+F delphi IDE RAD 丢失的快捷键 Ctrl+Shift+F Find inFiles 为什么呢?RAD Berlin安装了cnpack1.1.0.829后文件 ...
- 第一模块第一章 review
---恢复内容开始--- 练习题: 1.简述编译型与解释型语言的区别,且分别列出你知道的那些属于编译型,哪些属于解释型 机器语言:由于计算机内部只能接受二进制代码,因此,用二进制代码0和1描述的指令称 ...
- 1 python 文件处理
1.打开文件open 函数 open函数最常用的使用方法如下:文件句柄 = open('文件路径', '模式',编码方式). encode='' 1.关于文件路径 #文件路径: 主要有两种,一种是使用 ...
- dowhile
public class TestDoWhile { /**do while 至少执行一次 先斩后奏 * 测试dowhile */ public static void main(String[] a ...
- keras各种优化方法总结 SGDmomentumnesterov
http://blog.csdn.net/luo123n/article/details/48239963 前言 这里讨论的优化问题指的是,给定目标函数f(x),我们需要找到一组参数x,使得f(x)的 ...
- swagger ui
You can pull a pre-built docker image of the swagger-ui directly from Dockerhub: docker pull swagger ...
- python+webdriver,选取Select下拉框中的值
在选择下拉框中的值时遇到了困难,用driver.find_element_by_id("").send_keys("")进行赋值不能成功获取下拉框中的值. ...
- hdoj1003 DP
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- python作业之修改用户配置文件
用户的配置文件如下 backend oldboy school school1 age 21 weight 210 qq 550176565 iphone 139987676backend oldgi ...
- Linux符号连接的层数过多
转:http://blog.csdn.net/ta893115871/article/details/7458869 创建符号链接的时候一定要使用绝对路径,例如:/usr/local/cxxt/con ...