跟踪喜欢Django应用程序

时间:2018-04-24 16:25:12

标签: python django login social-likes

我正在尝试创建一个跟踪喜欢(pokes)的网站,每次点击戳按钮时,会将戳数加1.同时我希望用户能够戳他们用户拨打了多少次。

这是我的代码。

HTML:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <a href="/logout">Logout</a>
        <form action="/home" method="GET">
            {% csrf_token %}
        <h1>Welcome! {{request.session.name}}
            People have poked you
            {{%for stick in pokes%}}
            {{stick.first_name}}
            {{%endfor%}}
        </h1>
    </form>
    </head>
    <body>
        <p>People who you may want to poke</p>
        <table>
            <thead><th>Name</th><th>Alias</th><th>Email Address</th><th>Poke History</th><th>Action</th></thead>
            {%for stick in poke%}
            <tr><td>{{stick.first_name}}{{stick.last_name}}</td> <td>{{stick.first_name}}</td> <td>{{stick.email}}</td> <td>{{stick.}}</td> <td><form action="/poke" method="POST"><button>Poke</button></form></td></tr>
            {%endfor%}
        </table>
    </body>

模型

- - 编码:utf-8 - -

来自将来导入unicode_literals

from django.db import models
import bcrypt
import re
from datetime import *
import datetime

EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$')
NAME_REGEX = re.compile(r'^[aA-zZ\s]+$')
# Create your models here.
class UserManage(models.Manager):
    def validate(self, postData):
        errors = {}
        if len(postData['first_name']) < 2:
            errors["First name field can be left blank"]="first_name"
        elif not NAME_REGEX.match(postData['first_name']):
            errors["This is not a valid first name. Try again."]="first_name"

        if len(postData['last_name']) < 2:
            errors["Last name cannot be left blank"]="last_name"

        elif not NAME_REGEX.match(postData['last_name']):
            errors["This is not a valid last name. Try again."]="last_name"


        if len(postData['email']) < 1:
            errors["Email cannot be left blank"]="email"

        elif not EMAIL_REGEX.match(postData['email']):
            errors["this is not a valid email try again"]="email"

        if (User.objects.filter(email=postData['email'])):
            errors['Email already in use']="email"
        print postData["email"]


        if len(postData['password']) < 8:
            errors["Passwords must at least 8 characters"]="password"

        if postData["password"] != postData["cpassword"]:
            errors["Passwords do not match"]="cpassword"
        return errors

    def loginvalidate(self, postData):
        errors = {}
        if len(postData['email'])<1:
            errors["Email field can not be blank"] = "email"

        if len(postData["password"])<8:
            errors["Password must be at least 8 characters" ] = "password"

        if len(self.filter(email=postData['email']))>0:
            #print 'TRUE for emails'
            currentuser =self.filter(email=postData['email'])[0]
            existingpwd = currentuser.password

            if not bcrypt.checkpw(postData["password"].encode(), existingpwd.encode()):
                    errors["Password does not match"] = "password"
        else:
            errors["Email does not match"] = "email" 
        return errors
class PokeManage(models.Manager):
    def pokevalidate(self,postData):
        errors=[]
        return errors
class User(models.Model):
    first_name = models.CharField(max_length=45)
    last_name = models.CharField(max_length=45)
    email = models.CharField(max_length=45)
    password = models.CharField(max_length=45)
    birthdate = models.DateField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = UserManage()
class Poke(models.Model):
    date=models.DateTimeField(auto_now=True)
    key=models.ForeignKey(User,related_name="click")
    joined=models.ManyToManyField(User,related_name="poked")
    poke=models.IntegerField()
    objects=PokeManage()

的观点:

#-*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render, redirect
from .models import User
from .models import Poke
from django.contrib import messages
import bcrypt

# Create your views here.
def index(request):            
    return render(request,'index.html')
def home(request):
    current_user = User.objects.get(id=request.session['user_id'])
    poke=User.objects.exclude(id=request.session['user_id'])
    pokes=Poke.objects.join(id=request.session['user_id'])
    context={
    'user':current_user,
    'poke':poke,
    'pokes':pokes,
    }

    return render(request,"homepage.html",context)

def register(request):
    errors = User.objects.validate(request.POST)
    #print 'this process works', request.POST
    if len(errors) > 0:
        for error in errors:
            messages.error(request, error)
        return redirect("/")
    else:
        hashpwd = bcrypt.hashpw(request.POST["password"].encode(), bcrypt.gensalt())
        newuser = User.objects.create(
            first_name=request.POST['first_name'],
            last_name=request.POST['last_name'],
            email=request.POST['email'],
            password=hashpwd)

        request.session['user_id'] = newuser.id
        request.session['name'] = newuser.first_name
        print "session info", newuser.id, newuser.first_name
        return redirect("/home")

def login(request):
    errors = User.objects.loginvalidate(request.POST)
    if len(errors) > 0:
        for error in errors:
            messages.error(request, error)
        return redirect("/")
    else:
        user = User.objects.filter(email=request.POST['email'])[0]
        request.session['user_id'] = user.id
        request.session['name'] = user.first_name
        return redirect("/home")
def logout(request):
    request.session.clear()
    print 'goodbye'
    return redirect('/')
def poke(request,id):
    return redirect('/home')

0 个答案:

没有答案