Django 1.5.4 专题11 file uploading and updating database schema

一.修改settings.py 的内容如下

Django 1.5.4 专题11 file uploading and updating database schema_第1张图片

二.修改article/models.py的内容如下

from django.db import models
from time import time

def get_upload_file_name(instance,filename):
    return "upload_files/%s_%s" % (str(time()).replace('.','_'),filename)

# Create your models here.
class Article(models.Model):
    title       =models.CharField(max_length=200)
    body        =models.TextField()
    pub_date    =models.DateTimeField('date published')
    likes       =models.IntegerField()
    thumbnail   =models.FileField(upload_to=get_upload_file_name)

    def __unicode__(self):
        return self.title
class Comment(models.Model):
    name    =models.CharField(max_length=200)
    body    =models.TextField()
    pub_date = models.DateTimeField('date published')
    article  = models.ForeignKey(Article)

三.修改article/forms.py的内容如下

from django import forms
from models import Article,Comment

class ArticleForm(forms.ModelForm):

    class Meta:
        model=Article
        fields=('title','body','pub_date','thumbnail')
class CommentForm(forms.ModelForm):

    class Meta:
        model=Comment
        fields=('name','body')
四.修改article/templates/article.html
{% extends "base.html" %}

{% block sidebar %}
<ul>
    <li><a href="/articles/all">Articles</a></li>
</ul>
{% endblock %}

{% block content %}
<h1> {{ article.title }} </h1>
<p> {{ article.body }} </p>
{% if artilce.thumbnail %}
    <p><img src="/static/assets/{{article.thumbnail}}" width="200"></p>
{% endif %}
<p> <a> {{ article.likes }} people liked this article </a> </p>
<p><a href="/articles/like/{{article.id}}">Like</a></p>

<h2>Comment</h2>
{% for c in article.comment_set.all %}
<p>{{c.name}}:{{c.body}}</p>
{% endfor %}
<p><a href="/articles/add_comment/{{article.id}}">Add Comment</a></p>
{% endblock %}

五.修改article/templates/create_article.html的内容如下

{% extends "base.html" %}
{% block sidebar %}
<ul>
    <li><a href="/articles/all">Cancel</a></li>
</ul>
{% endblock %}
{% block content %}
<form action="/articles/create/" method="post" enctype="multipart/form-data">{% csrf_token %}
    <ul> {{ form.as_ul}} </ul>
    <input type="submit" name="submit" value="Create Article">
</form>
{% endblock %}

六.修改article/views.py的内容如下

def create(request):
    if request.POST:
        form=ArticleForm(request.POST,request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/articles/all')
    else:
        form=ArticleForm()
    args={}
    args.update(csrf(request))
    args['form']=form

    return render_to_response('create_article.html',args)

七.转换数据库后端时导入初始数据执行

    syncdb 并 不能将模型的修改或删除同步到数据库;如果你修改或删除了一个模型,并想把它提交到数据库,syncdb并不会做出任何处理,所以要导出数据,再导入数据

    python manage.py dumpdata article --indent=4 >article.json

    python manage.py reset article; 清除数据

    python manage.py loaddata article.json 导入json数据

八.最终效果如下

Django 1.5.4 专题11 file uploading and updating database schema_第2张图片


你可能感兴趣的:(django)