跟大妈学Django Part - 11 (Django入门续集11)

跟大妈学Django Part - 10 (Django入门续集10)

今天想先做个profile,这样用户就能拥有自己的小盒子。

做之前,先把登录Django Admin 把之前设定的user给删了,不然后面会出现ERROR。

总之,之前已经建立过好几次App了,今天要飞快的闪过。

通道内建APP, 上次用错了指令
如果用django-admin建app的话,会在下面一级建档,很是麻烦。
最好是用以下指令建新的app

python3 manage.py startapp profiles

view里先简单的render出页面,现在的设定是主要先确保url能用

/workspace/how-do-you-do/profiles/views.py



from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from .models import UserProfile
# Create your views here.


@login_required
def profile(request):
    """ Display the user's profile. """
    template = 'profiles/profile.html'
    context = {}
    return render(request, template, context)

profile的model只需要显示用户,名字,邮件,如果没有用户资料的话,就生成一个

/workspace/how-do-you-do/profiles/models.py



from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver


# Create your models here.
class UserProfile(models.Model):
    """a user profile to main personal feelings"""
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    full_name = models.CharField(max_length=50, null=True, blank=False)
    email = models.EmailField(max_length=254, null=True, blank=False)

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


@receiver(post_save, sender=User)
def create_or_update_user_profile(sender, instance, created, **kwargs):
    """
    Create or update the user profile
    """
    if created:
        UserProfile.objects.create(user=instance)
    # Existing users: just save the profile
    instance.userprofile.save()
# /workspace/how-do-you-do/profiles/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.profile, name='profile'),
]
# /workspace/how-do-you-do/hdyd/urls.py

 path('profile/', include('profiles.urls')),

前端模板

/workspace/how-do-you-do/profiles/templates/profiles/profile.html



{% extends "base.html" %}

{% load static %}

{% block content %}
<!--Main Navigation-->
<header>

    {% include 'mood/nav.html' %}

    <!-- Background image -->
    <div id="intro" class="p-5 text-center bg-image shadow-1-strong ">
        <div class="mask bg-color ">
            <div class="d-flex justify-content-center align-items-center h-100">
                <div class="px-4 bg-light bg-gradient text-black-50">

                    <div class="my-4"></div>

                    <h4 class="border border-light my-4 p-4">
                        {{ user }} Profile
                    </h4>

                    <button 
                        type="button"
                        class="btn btn-outline-muted btn-lg m-2 text-black-50" 
                        href="#" 
                        role="button"
                    >
                        Stuffing
                    </button>
                                    
                    
                    <div class="my-4"></div>
                    
                </div>
            </div>
        </div>
    </div>
    <!-- Background image -->
    
</header>

<hr class="m-0" />

{% include 'mood/footer.html' %}

{% endblock %}

链接成功

Moody Box Django 实战专栏直达

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CrankyCat

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值