Implement some of the hacker news features in the Python Django framework _python

Source: Internet
Author: User
Tags install django pip install django virtual environment

Step-by-step instructions

This is a text version of the video available to people who prefer to read. We'll create a social news site similar to hacking or Reddit. It will be called "Steel rumors" as a place to share interesting rumors about "Iron Man" and vote on it.

Screen Video overview of the first part:

    • Goal
    • Virtual environment-Start from scratch!
    • Model Management-Dream work #78
    • Basic templates
    • General View-News list view and news detail view
    • Pagination-FREE!!


setting up a virtual environment

We will use Virtualenv and Virtualenvwrapper to build a virtual development environment. First make sure that you have installed them:

Mkvirtualenv Djangorocks

I'm using a variant Ubuntu system called Xubuntu in my screen video. But you should be able to repeat these steps with minimal changes in other operating systems.

Install Django (make sure you have the PIP installed):

  Pip Install django==1.5

You can also use Django 1.5.1. These methods and codes have not been tested in the latest Django version to work, so it is best to follow the version used in this tutorial.

Create Engineering and application

Create a project named Steelrumors:

Copy Code code as follows:

CD ~/projects
django-admin.py Startproject steelrumors
CD steelrumors
chmod +x manage.py

Open the steelrumors/settings.py file in your favorite editor. Locate and change the following (changes are shown in bold):

Copy Code code as follows:
' ENGINE ': ' Django.db.backends.sqlite3 '
' NAME ': ' Database.db ',

Finally Installed_apps = (' Django.contrib.admin ',

Next, modify the steelrumors/urls.py and uncomment the following lines:

From django.contrib Import admin
  admin.autodiscover ()
   
  urlpatterns = Patterns (',
    url (r ' ^admin/', include (Admin.site.urls)),
  )

Create Management Objects synchronously and enter management details:

 ./manage.py syncdb

Open a new tab or a new terminal and keep the server instance running (don't forget to post the djangorocks on this terminal):

  ./manage.py Runserver

Access the Admin page (typically http://127.0.0.1:8000/admin/) and log in.

To create a link application:

  ./manage.py Startapp Links

Enter the following two model classes into the links/models.py file:

 From django.db import models from
  django.contrib.auth.models import User
   
  class Link (models. Model):
    title = models. Charfield ("headline", max_length=100)
    submitter = models. ForeignKey (User)
    submitted_on = models. Datetimefield (auto_now_add=true)
    Rank_score = models. Floatfield (default=0.0)
    URL = models. Urlfield ("URL", max_length=250, blank=true)
    description = models. TextField (blank=true)
   
    def __unicode__ (self): return
      Self.title
   
  class Vote (models. Model):
    voter = models. ForeignKey (User)
    link = models. ForeignKey (Link)
   
    def __unicode__ (self): return
      "%s upvoted%s"% (Self.voter.username, self.link.title)

Establish the appropriate management class. Enter the following into the links/admin.py:

 From django.contrib Import admin
  from. Models import Link, Vote
   
  class linkadmin (admin. modeladmin): Pass
  admin.site.register (Link, linkadmin)
   
  class voteadmin (admin. modeladmin): Pass
  admin.site.register (Vote, Voteadmin)

Enter the following to links/views.py:

 From django.views.generic import ListView
  from. Models import Link, Vote
   
  class Linklistview (ListView):
    Model = Link

  Insert following lines intosteelrumor/urls.py: from
 
  links.views import Linklistview
  ...
  urlpatterns = Patterns (',
    url (r ' ^$ ', Linklistview.as_view (), name= ' home '),

Create a new template folder and enter the following in the steelrumors/templates/links/link_list.html:

<ol>
  {% for link in object_list%}
    <li>
    <a href= ' {{link.url}} ' >
     <b>{{ Link.title}}</b>
    </a>
    </li>
  {% endfor%}
  </ol>

Edit settings.py, add your two applications to the Installed_apps = (tail:

  ' Links ',
  ' steelrumors ',
  )

Create linked objects synchronously and enter some data into the Admin interface:

  ./manage.py syncdb

Add Brand

Create a generic basic template steelrumors/templates/base.html:

  
 

Modify the steelrumors/templates/links/link_list.html to wrap the original code in this way:

 
  {% extends ' base.html '%}
   
  {% block content%}
  ...
  {% Endblock%}

Voting number Model Manager

We need to add a count of votes to our general ListView. Add these to links/models.py:

  From django.db.models import Count
   
  class Linkvotecountmanager (models. Manager):
    def get_query_set (self): return
      super (Linkvotecountmanager, self). Get_query_set (). Annotate (
        Votes=count (' vote ')). Order_by ('-votes ') Insert these two to the

  Link class lines:
 
  Class Link (models. Model):
  ...
   
    With_votes = Linkvotecountmanager ()
    objects = models. Manager () #default Manager

Edit the links/views.py and add the two lines to the Linklistview class:

 Class Linklistview (ListView):
  ...
   
    Queryset = Link.with_votes.all ()
    paginate_by = 3

Carnival (Cheating)

You can add 100 votes to a random caption in the Django Shell using the following line:

$./manage.py Shell
>>> links.models Import Link
>>> for I in xrange (MB): Vote (Link=link. Objects.order_by ('? ') [0],voter=a). Save ()

Final comment

Perhaps you would like to know if this version of this site is useful, and I would like to say that it works well as a personal beta. Any new user must be manually added through the admin interface. If you want them to log on to the admin interface, they must be employees. Employees can vote by manually creating a poll object.

The site's public-facing section still shows rumors of the highest voter turnout. Based on the question of how to design a good template, this version can also be used to gain feedback on the design and branding of the site.

Finish the first part of the summary. For the next part of the update, please powder me a @arocks on Twitter.

Related Article

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.