larken

勤奋的人生才有价值

导航

Part 3 - Advanced Concepts(11-13)

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

 

posted on 2018-09-06 19:02  larken  阅读(154)  评论(0编辑  收藏  举报