美文网首页django2.1 博客系统
九、Django2.1 搭建多用户的博客网站——用户模块小结

九、Django2.1 搭建多用户的博客网站——用户模块小结

作者: 27efec53a72d | 来源:发表于2019-01-29 08:06 被阅读27次

    目录:Django 2.1 从零开始搭建博客网站系列

    服务器环境搭建(选学)

    小试牛刀——简单的博客网站

    庖丁解牛——多用户的博客网站之用户模块

    庖丁解牛——多用户的博客网站之文章模块

    华丽转身——多用户的博客网站之扩展功能

    项目源码下载:https://github.com/jt1024/lehehe

    正文:

    通过前面6篇文章,我们已经比较完整地实现了一个多用户博客网站的用户管理模块。

    目前各个环节的页面跳转还缺少一些衔接,我们继续完善一下。

    1、完善导航栏

    修改 ./templates/header.html ,完整代码如下

    {% load staticfiles %}
    <div class="container">
        <nav class="navbar navbar-default" role="navigation">
            <div class="navbar-header">
                <a class="navbar-brand" href="http://www.taoge100.com"><img src="{% static '/images/logo.png' %}" width="100px"></a>
            </div>
            <div>
                <ul class="nav navbar-nav" role="navigation">
                    <li><a href="{% url 'blog:blog_list' %}">BLOG</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right" style="margin-right:10px">
                    {% if user.is_authenticated %}
                    <li>
                        <div class="dropdown" style="margin-top:8px">
                            <button class='btn btn-default dropdown-toggle' type='button' id='dropdownMenu' data-toggle='dropdown'>
                                {{ user.username }}<span class='caret'></span>
                            </button>
                            <ul class="dropdown-menu">
                                <li><a href="{% url 'account:my_information' %}">个人信息</a></li>
                                <li><a href="{% url 'account:edit_my_information' %}">完善信息</a></li>
                                <li><a href="{% url 'account:password_change' %}">修改密码</a></li>
                                <li><a href="{% url 'account:password_reset' %}">密码重置</a></li>
                            </ul>
                        </div>
                    </li>
                    <li><a href="{% url 'account:user_logout' %}">Logout</a></li>
                    {% else %}
                    <li><a href="{% url 'account:user_login' %}">Login</a></li>
                    <li><a href="{% url 'account:user_register' %}">Sign up</a></li>
                    {% endif %}
                </ul>
            </div>
        </nav>
    </div>
    
    <script src="{% static 'js/jquery.js' %}"></script>
    <script src="{% static 'js/bootstrap.js' %}"></script>
    

    运行Django,登录后页面如图:


    完善导航栏.png

    2、设置默认主页

    修改 ./lehehe/urls.py ,完整代码如下

    from django.contrib import admin
    from django.urls import path, include
    from django.views.generic import TemplateView
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('blog/', include('blog.urls')),
        path('account/', include('account.urls')),
        path('home/', TemplateView.as_view(template_name="home.html"), name='home'), # 新增
    ]
    

    创建 ./templates/ home.html

    {% extends "base.html" %}
    {% block title %} Home page {% endblock %}
    {% block content %}
    <div class="text-center">
        <h1>WELCOM YOU</h1>
        <h3>Life is short. You need Python</h3>
        <h2>Django makes it easier to build better Web apps more quickly and with less code.</h2>
        {% load staticfiles %}
        <img src="{% static 'images/lehehe.png' %}" width="400px">
    </div>
    {% endblock %}
    

    修改 ./templates/header.html ,在

    <li><a href="{% url 'blog:blog_list' %}">BLOG</a></li> 
    

    上面增加一行代码:

    <li><a href="{% url 'home' %}">HOME</a></li>
    

    运行Django,登录后,点击导航栏中的“HOME”或直接访问http://127.0.0.1:8000/home/ 页面如图:

    HOME.png

    3、完善登录/注册跳转链接

    3.1 修改登录后的跳转链接

    在 ./lehehe/settings.py 中修改 LOGIN_REDIRECT_URL 的值:

    LOGIN_REDIRECT_URL = '/home/'
    

    3.2 修改注册后的跳转链接

    在 ./account/views.py 中修改 user_register 方法,完整代码如下:

    def user_register(request):
        if request.method == "POST":
            user_form = RegistrationForm(request.POST)
            userprofile_form = UserProfileForm(request.POST)
            if user_form.is_valid() * userprofile_form.is_valid():
                new_user = user_form.save(commit=False)
                new_user.set_password(user_form.cleaned_data['password'])
                new_user.save()
                new_profile = userprofile_form.save(commit=False)
                new_profile.user = new_user
                new_profile.save()
                UserInfo.objects.create(user=new_user)  # 新增,增加myself方法后,才添加这行代码
                # return HttpResponse("注册成功!")
                return HttpResponseRedirect(reverse("account:user_login"))
            else:
                return HttpResponse("抱歉,你不能注册")
        else:
            user_form = RegistrationForm()
            userprofile_form = UserProfileForm()
            return render(request, "account/register.html", {"form": user_form, "profile": userprofile_form})
    

    大功告成!此时再点击导航栏的各个按钮,测试登录、注册等操作看看效果如何。

    相关文章

      网友评论

        本文标题:九、Django2.1 搭建多用户的博客网站——用户模块小结

        本文链接:https://www.haomeiwen.com/subject/fmytsqtx.html