Django Development BBS---51 Web Course notes (1)

Source: Internet
Author: User

51 has a free video on developing BBS forums with Django, write a brief note on the development process. Course Address: http://edu.51cto.com/course/course_id-2787.html

The Forum was developed with the "drawer" http://dig.chouti.com/as its prototype. Develop a similar BBS website.


The main interface structure of the drawer website:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/C1/wKiom1WFFcqiCJUDAAMLUgL25lg778.jpg "title=" Qq20150620152628.jpg "alt=" Wkiom1wffcqicjudaamlugl25lg778.jpg "/>


Thus, before you create the project, determine the table structure of the data:

First, there should be a table for posting,

Second, which user is sent, should create a user table

Again, there should be a comment form. An ID in the comment table is attached to the post table

Plate tables should also be established.


In the Post table, you should have the following data structure.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/BF/wKiom1WEytbygUr9AAFRXAXnNsg202.jpg "title=" Qq20150620100647.jpg "alt=" Wkiom1weytbygur9aafrxaxnnsg202.jpg "/>

The meaning of the data in the table will be explained in the code snippet, which is not explained in detail here.


In the comments form, you should have the following information:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BC/wKioL1WEzWmzQVNBAAHQ82y1VyU036.jpg "title=" Qq20150620101035.jpg "alt=" Wkiol1wezwmzqvnbaahq82y1vyu036.jpg "/>

Here, we use the structure of the comment table that comes with Django. This table is canceled in Django1.8, so the new version of Django does not have this table, and you can define the table structure yourself.


Then the user table:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BF/wKiom1WEzJmQn1s-AADYgesfIAM461.jpg "title=" Qq20150620101428.jpg "alt=" Wkiom1wezjmqn1s-aadygesfiam461.jpg "/>

and finally the plate table:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/BC/wKioL1WEzunidKv2AACwXLyjYzE840.jpg "title=" Qq20150620101708.jpg "alt=" Wkiol1wezunidkv2aacwxlyjyze840.jpg "/>



1. Create the project Bbs_project:

In the folder you want to create the project with shift+ right-click, open the Command window, enter:

django-admin.py Startproject Bbs_project

Create a project

Under this project, create a APP01 application and enter:

Python manage.py Startapp App01


In Django, models can describe the SQL of the database in the form of Python code and automatically generate a table in the database. To create a model of the data table in APP01, first add the APP01 to the installed configuration file in order to apply the excitation:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/C0/wKiom1WE2cyhY35iAAHsi1yhOHw779.jpg "title=" Qq20150620111037.jpg "alt=" Wkiom1we2cyhy35iaahsi1yhohw779.jpg "/>

For the specific inside of model, you can refer to: http://4440271.blog.51cto.com/4430271/1656532


Here, you also need to configure the database information

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/C0/wKiom1WE2ynB1KtNAAFPdfXox1Y247.jpg "title=" Qq20150620111628.jpg "alt=" Wkiom1we2ynb1ktnaafpdfxox1y247.jpg "/>


databases = {     ' Default ': {          ' engine ':  ' Django.db.backends.mysql ',   # django which database engine to use           ' NAME ':  ' Bbs_pro ',                       #  tell the database name  Django          ' USER ':  ' root ',                         #  Tell  Django  which user is connected to the database          ' PASSWORD ':  ' ********* ',                #  Tell the Django connection user's password          ' HOST ':  ' 127.0.0.1 ',                    #  tell  Django  which host the database server is connected to           ' PORT ':  ' 3306 ',                         #  Port number      }}


In models.py, add the code that creates the database table: specific description See: http://4440271.blog.51cto.com/4430271/1656532


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/C1/wKiom1WFFo2w8u3sAACD_gueAv8615.jpg "title=" Qq20150620152949.jpg "alt=" Wkiom1wffo2w8u3saacd_gueav8615.jpg "/>

The code is as follows:

# coding=utf-8from django.db import modelsfrom django.contrib.auth.models import  User# Create your models here.#  CREATE TABLE #  post table Class bbs (models. Model):     category = models. ForeignKey (' Category ')  #  posts on the plate      title = models. Charfield (max_length=64)   #  post title, title can be repeated     summary = models. Charfield (max_length=256, blank=true, null=true)   #  Post Introduction      Content = models. TextField ()   #  reviews     author = models. ForeignKey (' Bbs_user ')   #  author     view_count = models. Integerfield ()   #  browse number     ranking = models. Integerfield ()   #  ranking     created_at = models. Datetimefield (auto_now_add=true)   #  creation Date     updated_at = models. Datetimefield (auto_now_add=true)   #  Date Modified     def __unicode__ (self):         return self.title#  Module Table   Moderator Class category ( Models. Model):     name = models. Charfield (max_length=32, unique=true)   #  plate name     administrator =  models. ForeignKey (' Bbs_user ')      #  moderator     def __unicode__ (self ):         return self.name# bbs user table Class bbs_user (models . Model):     user = models. Onetoonefield (User)   #  one-to-one foreign key     signature = models. Charfield (max_length=128, default= ' this guy is too lazy to leave  Anything here ')   #  personality Signature   &nbsP; photo = models. ImageField (upload_to= "upload_imgs/",  default= "upload_imgs/user1.jpg")   #  avatar picture      def __unicode__ (self):        return  Self.user.username


In this model, we have added

def __unicode__ (self): return self.user.username


This line of code, the role of this code will be displayed in the site management:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/C1/wKiom1WFMGOTFTsJAAAfZLtNj50098.jpg "title=" Qq20150620171853.jpg "alt=" Wkiom1wfmgotftsjaaafzltnj50098.jpg "/>

The user name is shown here



In this code, we didn't write the code for Commens, the comment table, because the Django-brought comment sheet was used.

To join the Django-comments table, you need to include it in the settings.py installed directory:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/BD/wKioL1WFLkDDQZaYAAEpNckK2fI739.jpg "title=" Qq20150620170341.jpg "alt=" Wkiol1wflkddqzayaaepnckk2fi739.jpg "/>

Add the value of site_id at the same time:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BD/wKioL1WFLsuRuQLqAAB0fEWHu2A916.jpg "title=" Qq20150620170557.jpg "alt=" Wkiol1wflsuruqlqaab0fewhu2a916.jpg "/>

Once added, you can verify the validity of the model with the following code:

Python manage.py Validate

Then synchronize the database:

Python manage.py syncdb

A pillow package is required here, as prompted to install in the Python directory: Pip install pillow.


After the creation is complete, run the server:

Python manage.py runserver

Open in Browser

http://127.0.0.1:8000/admin/login/?next=/admin/


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/C1/wKiom1WFHwuwCbIKAAByiJ1cyoc290.jpg "title=" Qq20150620160406.jpg "alt=" Wkiom1wfhwuwcbikaabyij1cyoc290.jpg "/>


Enter User name password login:

Here's a bit of a wordy. How to create a super User:

On the command line, enter: Python manage.py createsuperuser

Then at the prompt to enter the user name, mailbox, password, you can

Fill in the information in the user name and password you just entered



In admin.py, add the model you want to manage in the background:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/C1/wKiom1WFKjLyJuG3AABb-TyT6po232.jpg "title=" Qq20150620165338.jpg "alt=" Wkiom1wfkjlyjug3aabb-tyt6po232.jpg "/>


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6E/BD/wKioL1WFLDaxfqVmAACrOPdpQAI666.jpg "title=" Qq20150620165506.jpg "alt=" Wkiol1wfldaxfqvmaacropdpqai666.jpg "/>

# coding=utf-8from Django.contrib Import adminfrom app01 import modelsclass bbs_admin (admin. Modeladmin): List_display = (' title ', ' summary ', ' author ', ' Signature ', ' view_count ', ' created_at ') List_filter = (' Created_at ',) Search_fields = (' title ', ' Author__user__username ') # Displays the signature def signature in the author table in the BBS table (self , object): Return object.author.signature# Register your models Here.admin.site.register (models. BBS, Bbs_admin) Admin.site.register (models. Category) Admin.site.register (models. Bbs_user)


At the command line, enter:

Python manage.py makemigrations App01

And then

Python manage.py Migrate

You can change the table dynamically.


To add a URL configuration in url.py:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BE/wKioL1WFSZ2zkevEAAHptpvNxLE240.jpg "title=" Qq20150620185929.jpg "alt=" Wkiol1wfsz2zkeveaahptpvnxle240.jpg "/>

In this way, the model to be managed is displayed: specific reference: http://4440271.blog.51cto.com/4430271/1656624

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BD/wKioL1WFK3zxPszTAAEvcM_auP0464.jpg "title=" Qq20150620165148.jpg "alt=" Wkiol1wfk3zxpsztaaevcm_aup0464.jpg "/>

You can add data to it

In the admin in the Django display to do some customization, this code in the previous admin.py has been given, here in detail:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BE/wKioL1WFNI3QkGMkAAKkzSfqrNM513.jpg "title=" Qq20150620173039.jpg "alt=" Wkiol1wfni3qkgmkaakkzsfqrnm513.jpg "/>

The effect after adding

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6E/BE/wKioL1WFNDLSc_LbAANY8TeonGE018.jpg "title=" Qq20150620172845.jpg "alt=" Wkiol1wfndlsc_lbaany8teonge018.jpg "/>


Next, to join the front-end design, this section gives







This article is from the "Thystar" blog, make sure to keep this source http://4440271.blog.51cto.com/4430271/1663863

Django Development BBS---51 Web Course notes (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.