Django first experience-building a simple blog

Source: Internet
Author: User
Tags create blog

A few days ago on the internet saw the use of Django to build a simple blog video, Curiosity driven to go to learn the next, after all, their own for Django is extremely awe, not very understanding, to the beginning of the experience.

The operating environment of this article: Ubuntu, python2.7, Django1.8.6, PYCHARM5. In fact, since the use of Ubuntu has been very magical like it, really easy to use a lot.

1.Django project file and create blog app

(1) You can set up a Django project in the terminal, using Django-admin startproject ... Direct:

Build a blog app:

(2) Creating a Django project in Pycharm

To download and install Pycharm, select New Project and build the project directly:

This establishes a project, in fact, compared to VIM, choose pycharm more convenient, its indentation and auto-add function is really great.

Well established we can see:

2. Create a simple project

In the Django project you created, open the views.py and use the simple actions in HTML to write:

From django.shortcuts import renderfrom django.http import httpresponsedef Hello (Request):    return HttpResponse (' 

In conjunction with regular expressions, the following modifications can be made in urls.py:

Urlpatterns = [    url (R ' ^blog/$ ', ' Blog.views.hello '),    url (r ' ^admin/', include (Admin.site.urls)),
]

Open the browser, type: localhost:8000/blog/, enter:

This gives a simple understanding of Django's operations. Of course, combined with the relevant knowledge of HTML, you can also make certain changes:

Establish hello.html in templates:

<! DOCTYPE html>

Make the following changes in the views:

def hello (request):    name = ' Benben ' age    =    return render (Request, ' hello.html ', locals ())

Open Localhost:8000/blog:

3. Models Database Mapping

To build a blog, then it is bound to have the author and the article Two simple objects, established in models.py:

From django.db import models# Create your models Here.class Author (models. Model):    name = models. Charfield (max_length=100) Age    = models. Integerfield (MAX_LENGTH=18) class article (models. Model):    title=models. Charfield (max_length=200)    content = models. TextField ()    url=models. Urlfield ()    portal = models. ImageField ()    author = models. ForeignKey (Author)

In Pycharm tools, select the Run manage.py task input migrate, such as:

View the built database in the terminal:

The reason for this is sqlite3 is in setting.py:

Of course, a friend who needs to use MySQL can also set up MySQL here.

4.admin Background Management

The admin.py is automatically generated, and the previous two classes article and author are established here:

From Django.contrib import adminfrom blog.models import *# Register your models Here.admin.site.register (article) Admin.site.register (Author)

After running the server, enter SYNCDB in the Run manage.py task to connect to the database:

Enter Yes, then the name of the mailbox password, such as their own settings, open the browser, input localhost:8000/admin, enter:

Enter the set user and password:

In this way, we have completed a blog with the simplest features that can be accessed backstage to create an article:

On the whole, there's a lot more to learn, and there's a simple understanding of Django. Always doing the things that you like, well, want to keep going on ...

Django first experience-building a simple blog

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.