Django 写博客1. 一个网页,两个form,分别提交

Posted joeyfang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django 写博客1. 一个网页,两个form,分别提交相关的知识,希望对你有一定的参考价值。

Django新手,目前断断续续学了一个月Django,开始写个博客

Python 2.7.11  Django 1.10.2  Bootstrap 3.3.0   IDE:eclipse Pydev

models

# 用户
class User(models.Model):
    username = models.CharField(max_length=50, unique=True)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=50)
    admin = models.BooleanField(default=False)
    #image = models.ImageField(upload_to=‘static/image/%Y/%m‘, default=‘static/image/default.jpg‘, max_length=200,
    #                          blank=True, null=True, verbose_name=‘用户头像‘)
    created_dt = models.DateTimeField(auto_now_add=True, db_index=True)

    def __unicode__(self):
        return self.username


# 博客分类
class Category(models.Model):
    name = models.CharField(max_length=64, blank=True, null=True)

    def __unicode__(self):
        return self.name


# 博客标签
class ArticleTag(models.Model):
    tagname = models.CharField(max_length=48, blank=True, null=True)

    def __unicode__(self):
        return self.tagname


# 博客
class Article(models.Model):

    subject = models.CharField(max_length=50)
    summary = models.CharField(max_length=500)
    content = models.TextField()
    created_dt = models.DateTimeField(auto_now_add=True, db_index=True)
    modify_dt = models.DateTimeField(auto_now=True, null=True, blank=True)
    article_type = models.ForeignKey(Category)
    article_tag = models.ManyToManyField(ArticleTag)
    writer = models.ForeignKey(User)
    is_top = models.BooleanField(default=False)
    num_like = models.PositiveIntegerField(default=0)
    num_click = models.PositiveIntegerField(default=0)

    def get_absolute_url(self):
        return reverse(article-detail, kwargs={pk: self.pk})

    def __unicode__(self):
        return self.subject


# 评论
class Comment(models.Model):

    content = models.TextField()
    created_dt = models.DateTimeField(auto_now_add=True, db_index=True)
    user = models.ForeignKey(User)
    article = models.ForeignKey(Article)
    num_like = models.PositiveIntegerField()
    num_dislike = models.PositiveIntegerField()
    pid = models.ForeignKey(self, blank=True, null=True, verbose_name=父评论)

 

 一 实现用户管理功能(展示用户信息(userinfo),修改用户密码(changepwd),修改用户信息(changeuserinfo))

 在一个页面下不同标签切换显示:

 1 <div id="bodynav" class="nav nav-tabs">
 2         <ul class="nav nav-tabs" role="tablist">
 3             <li role="presentation" class="active"><a href="#tab_userinfo" data-toggle="tab">用户信息</a></li>
 4             <li role="presentation"><a href="#tab_changepwd" data-toggle="tab">密码修改</a></li>
 5             <li role="presentation"><a href="#tab_changeuserinfo" data-toggle="tab">用户信息修改</a></li>
 6         </ul>
 7     </div>
 8     <!-- 每个tab页对应的内容 -->  
 9     <div id="myTabContent" class="tab-content"> 
10         <div class="tab-pane fade in active" id="tab_userinfo">
11             {{userinfo.username}}
12             {{userinfo.email}}
13         </div>
14 
15         <div class="tab-pane fade" id="tab_changepwd">
16             <form id=‘changepwd‘ action="/changepwd/" method="post">
17                 <table border =2>
18                     {{changeform.as_table}}
19                 </table>
20                 <input type="submit"  name=‘changepwd‘ value="确认" >
21             </form>
22         </div>
23 
24         <div class="tab-pane fade" id="tab_changeuserinfo">
25                 <form id=‘changeuserinfo‘ action="/changeuserinfo/" method="post">
26                     <table border =2>
27                         {{newuserinfo.as_table}}
28                     </table>
29                     <input type="submit"  name=‘changeuserinfo‘ value="确认" >
30                 </form>
31         </div>
32     </div>  

同时根据不同表单分别提交不同到的views处理

以上是关于Django 写博客1. 一个网页,两个form,分别提交的主要内容,如果未能解决你的问题,请参考以下文章

django -- 实现ORM登录

CSS:仿写博客园写一个静态网页

Python自动化开发学习22-Django下(Form)

自己写一个网页版的Markdown实时编辑器

Django表单提交数据与网页跳转

Django-1