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)的更多相关文章

  1. Notes of Daily Scrum Meeting(11.13)

    Notes of Daily Scrum Meeting(11.13) 今天邹欣老师给我们讲课大家还是很有收获的,大家课堂的参与度确实有了很大的提升,而且邹欣老师关于项目Scrum Meeting报告 ...

  2. The 10th Shandong Provincial Collegiate Programming Contest(11/13)

    $$The\ 10th\ Shandong\ Provincial\ Collegiate\ Programming\ Contest$$ \(A.Calandar\) 签到 //#pragma co ...

  3. (11.13)Java小知识!

    今天想要与大家分享一下有关于构造方法的知识! 构造方法的定义与作用 构造方法是一种特殊类型的方法.当一个对象被创建的时候,构造方法用来初始化对象,也就是说构造方法其实是一个名词而不是动词,像我刚刚开始 ...

  4. 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. ...

  5. VR/AR软件—Mirra测试(截至2017/11/13),使AR/VR创作更加便捷

    Mirra(截至2017/11/13)https://www.mirra.co/ 1.主要特点: 目前仅支持VR,不支持AR 在浏览器(仅支持chrome,firefox)上进行创作,但目前不能直接在 ...

  6. (Problem 13)Large sum

    Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072875339 ...

  7. (1.13)mysql优化数据库对象

    (1.13)mysql优化数据库对象 1.mysql优化数据库对象 [1.1]数据库对象类型优化 select * from test1 procedure analyse(); ,); --不要为那 ...

  8. (3.13)mysql基础深入——mysql日志分析工具之mysqlsla【待完善】

    (3.13)mysql基础深入——mysql 日志分析工具之mysqlsla 关键字:Mysql日志分析工具.mysqlsla 常用工具 [1]mysqldumpslow:官方提供的慢查询日志分析工具 ...

  9. Notes of Daily Scrum Meeting(11.19)

    Notes of Daily Scrum Meeting(11.19) 现在工程项目进入尾声了,我们的项目中还有一些问题需要解决,调试修改起来进度比较慢,所以昨天就没有贴出项目 进度,今天的团队工作总 ...

  10. Notes of Daily Scrum Meeting(11.17)

    Notes of Daily Scrum Meeting(11.17) 今天是第四周的周一,也就是说距离最后发布也只剩下一周的时间,但我们的工程里面还有很多的问题没有解决,我关注过 其他一两个小组,他 ...

随机推荐

  1. VBA 连接文本的自定义函数(可用于数组公式)

    Function ConTxt(ParamArray args() As Variant) As VariantDim tmptext As Variant, i As Variant, cellv ...

  2. Excel函数使用

    Excel函数使用: count&countif today&now count count() 仅可以统计数字类型的单元格个数 countif 根据条件来选择统计 countif(D ...

  3. UI5-文档-4.29-Integration Test with OPA

    如果我们想测试我们的应用程序的交互模式或更多的可视化特性,我们也可以编写一个集成测试. 我们还没有想过测试我们与app的交互,所以在这一步中,我们将在点击“Say Hello with dialog” ...

  4. 常用HTTP状态码

    1.常用状态码介绍 在http响应协议中,我们通过HttpWatch抓包抓取到响应信息.其中响应首行中就包含一个状态码.状态码由三位数字组成,表示请求是否被理解或者被满足.HTTP响应状态码的第一个数 ...

  5. c语言使用librdkafka库实现kafka的生产和消费实例(转)

    关于librdkafka库的介绍,可以参考kafka的c/c++高性能客户端librdkafka简介,本文使用librdkafka库来进行kafka的简单的生产.消费 一.producer librd ...

  6. sql查询分析器中显示行号

    -- 工具-> -- 选项-> -- 文本编辑器-> -- 所有语言-> -- 常规-> -- 显示-> -- 行号

  7. Rquest对象代码练习

    1.代码练习 <%@ page language="java" import="java.util.*" pageEncoding="utf-8 ...

  8. Numpy统计

    Numpy统计 axis=None 是统计函数的标配参数,默认不输入此参数则为对数组每一个元素进行计算,设定轴则对此轴上元素进行计算 1:常用统计函数 .sum(a,axis=None):数组a求和运 ...

  9. rook 记录

    更改rook 集群的配置 https://github.com/rook/rook/blob/master/design/cluster-update.md rook集群升级流程 https://ro ...

  10. fiddler 发送get请求

    点击Composer 点击执行(Execute) \ 这里演示的是带cookie