首页 > 代码库 > Django项目

Django项目

一. 相亲小项目

用户登录  如果男用户登录,显示女生列表
             如果女用户登录,显示男生列表
技术分享
from django.shortcuts import render,HttpResponse,redirect
from app01 import models



def login(request):
    if request.method == "GET":
        return render(request,"login.html")
    else:
        username = request.POST.get("username")
        password = request.POST.get("password")
        gender = request.POST.get("gender")
        if gender == "1":
             obj = models.Boy.objects.filter(username=username,password=password).first()
        else:
            obj = models.Girl.objects.filter(username=username,password=password).first()
        if not obj:
            #未登录
            return render(request,"login.html",{"msg":"用户名或密码错误"})
        else:
            request.session["user_info"] = {"user_id":obj.id,"gender":gender,"username":username,"nickname":obj.nickname}
        return redirect("/index.html")



def logout(request):
    if request.session.get("user_info"):
        request.session.clear()
    return redirect("/login.html")
views/account.py
技术分享
from django.shortcuts import render,HttpResponse,redirect
from app01 import models


def index(request):
    if not request.session.get("user_info"):
        return redirect("/login.html")
    else:
        gender = request.session.get("user_info").get("gender")
        if gender == "1":
            user_list = models.Girl.objects.all()
        else:
            user_list = models.Boy.objects.all()
        return render(request,"index.html",{"user_list":user_list})


def others(request):

    current_user_id = request.session.get("user_info").get("user_id")
    gender = request.session.get("user_info").get("gender")
    if gender == "1":
        user_list = models.B2G.objects.filter(b_id=current_user_id).values("g__nickname")
    else:
        user_list = models.B2G.objects.filter(g_id=current_user_id).values("b__nickname")
    return render(request,"other.html",{"user_list":user_list})



def test(request):

    # models.Boy.objects.create(nickname="方少伟",username="fsw",password="123")
    # models.Boy.objects.create(nickname="陈涛",username="ct",password="123")
    # models.Boy.objects.create(nickname="egon",username="egon",password="123")
    #
    # models.Girl.objects.create(nickname="lili", username="lili", password="123")
    # models.Girl.objects.create(nickname="jim", username="jim", password="123")
    # models.Girl.objects.create(nickname="xiaojie", username="xiaojie", password="123")

    # models.B2G.objects.create(b_id=1,g_id=1)
    # models.B2G.objects.create(b_id=1,g_id=2)
    # models.B2G.objects.create(b_id=1,g_id=3)
    # models.B2G.objects.create(b_id=2,g_id=1)
    # models.B2G.objects.create(b_id=3,g_id=1)
    # models.B2G.objects.create(b_id=4,g_id=1)

    return HttpResponse("...")
views/love.py
技术分享
from django.db import models

# Create your models here.


class Boy(models.Model):
    nickname = models.CharField(max_length=32)
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=64)

class Girl(models.Model):
    nickname = models.CharField(max_length=32)
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=64)


class B2G(models.Model):
    b = models.ForeignKey(to="Boy",to_field="id")
    g = models.ForeignKey(to="Girl",to_field="id")
models.py
技术分享
urlpatterns = [
    url(r^admin/, admin.site.urls),

    url(r^test.html$, love.test),


    url(r^login.html$, account.login),
    url(r^logout.html$, account.logout),

    url(r^index.html$, love.index),
    url(r^others.html$, love.others),

]
urls.py
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <form action="/login.html" method="POST">
        {% csrf_token %}
        <p>用户:<input type="text" name="username"></p>
        <p>密码:<input type="password" name="password"></p>
        <p>
            性别:
                男<input type="radio" name="gender" value="1"><input type="radio" name="gender" value="2">
        </p>

        <input type="submit" value="提交">{{ msg }}
    </form>


</body>
</html>
login.html
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% include "user_head.html" %}


<h3>异性列表</h3>
<a href="/others.html">查看和我有关系的异性</a>
<ul>
    {% for row in user_list %}
        <li>{{ row.nickname }}</li>
    {% endfor %}
</ul>

</body>
</html>
index.html
技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    {% include "user_head.html" %}


    <h3>有关系的异性列表</h3>
    <ul>
        {% for row in user_list %}
            {% if  row.g__nickname %}
                <li>{{ row.g__nickname }}</li>
            {% else %}
                <li>{{ row.b__nickname }}</li>
            {% endif %}
        {% endfor %}
    </ul>


</body>
</html>
other.html
技术分享
<h3>当前用户: {{ request.session.user_info.nickname }}</h3>
<a href="/logout.html">注销</a>
user_head.html

 

Django项目