Python入门到逆袭14(项目篇-web项目3)

7. 用户管理

7.1 初始化用户

此时还没有用户密码可以登录

 

备注 : 如果页面显示不正常,则修修改下如下配置(原因是static路径错误)

/usr/local/nginx/conf/nginx.conf

setting.py

  • 通过如下命令,创建一个初始账号

python manage.py createsuperuser

账号密码:  admin/test1234

创建后auth_user表中多了一条记录

  • 登录后为

7.2 用户认证模块改写

一般来讲,项目中都不会用django自己的登录认证模块,都会重新选择一个开源的或者自己实现一个认证模块来代替,感兴趣的可以去研究一下。

 

8. 模块注册

我们需要实现的页面和管理系统类似,主要为实现数据表的增删查改功能,通过django的功能,将对应的操作配置一个页面,来进行数据表的操作。

在6.2章节中,定义类models模型,只需要将类注册到admin中,页面对应就会出现栏目。

 

8.1 关键字模块注册

mysite/testweb/admin.py

from django.contrib import admin
from django.utils.html import format_html
# Register your models here.

from .models import CrawlerConf

class CrawlerConfAdmin(admin.ModelAdmin):
    '''
    关键字配置页面注册
    '''
# 页面显示的字段
    list_display = ('keyword', 'start_page', 'end_page', 'detail')

    # 配置查询字段
    search_fields = ('keyword',)
    
    # 配置排序
    ordering = ('id',)

    # 配置在新增,编辑页面中显示的字段
    fields = ('keyword', 'start_page', 'end_page')

    def detail(self, obj):
        #return '<a href="%s%s">%s</a>' % ('http://url-to-prepend.com/', obj.url_field, obj.url_field)
        detail_html = u'<a href="/crawler/">详情</a>'
        return format_html(detail_html)

    detail.allow_tags = True
    detail.short_description = 'detail'


admin.site.register(CrawlerConf, CrawlerConfAdmin)

替换代码后重启,结果如下:

代码解释:

list_display : 显示的列名

search_fields : 搜索字段

ordering : 排序

detail : 详情字段,是一个超链接,点击进去,跳转到结果列表

fields: 点解keyword列的关键字,进入编辑修改页面,fileds中的字段为能够修改的字段。

由于这个是django自己的模块注册机制,所以基本上不需要些什么代码,就能实现这块的逻辑。

 

8.2 结果展示模块

其实这个模块也可以用django的注册方式实现,但是主要是用于学习,就换成另外一种模板的方式,毕竟django注册出来的页面太丑了是不。

先上个图吧,虽然比上面的好看点,但是也没好看到哪里去,主要是我不太懂前端,但是这个可操作性就很强,懂前端越多,做出来就越好看。

8.2.1 url路由

为什么访问http://ip/crawler/会出现上面的页面。

是因为注册了 /crawler/ 的url路由。

 

mysite/mysite/url.py:

......
urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^crawler/', include('testweb.urls')),
]

mysite/testweb/url.py:

from django.conf.urls import  url
from testweb import views
    
urlpatterns = [
    url(r'^$', views.IndexView.as_view(), name='crawler-home'),
    url(r'^detail/$', views.DetailView.as_view(), name='crawler_detail'),
]

解释 :

url(r'^crawler/', include('testweb.urls')),  包含二级url目录,会去解析testweb/url.py文件中的urlpatterns 列表中的路由,当前根目录为/crawler/

url(r'^$', views.IndexView.as_view(), name='crawler-home'), /crawler/下的根目录,对应的视图为IndexView,只要实现IndexView就可以访问对应的url路径了。

 

8.2.2 视图

testweb/view.py

......
class IndexView(generic.ListView):
    '''

    '''

    template_name = 'testweb/crawler_list.html'
    context_object_name = 'crawler_list'

    def get_queryset(self):
        res = []

        crawler_list = CrawlerConf.objects.all()
        crawler_res_list = CrawlerRes.objects.all()

        for crawler_res in crawler_res_list:
            tmp_res = {}
            tmp_res['keyword'] = '-'
            keyword_info = crawler_list.filter(id=crawler_res.craw_id).first()
            if keyword_info:
                tmp_res['keyword'] = keyword_info.keyword

            tmp_res['host_ip'] = crawler_res.host_ip
            tmp_res['belong'] = crawler_res.belong
            tmp_res['main_dns'] = crawler_res.main_dns
            if len(crawler_res.link_dns) < 30:
                tmp_res['link_dns'] = crawler_res.link_dns
            else:
                tmp_res['link_dns'] = crawler_res.link_dns[0:30] + '......'
            tmp_res['id'] = crawler_res.id
            res.append(tmp_res)

        return res
......

代码解释:

template_name = 'testweb/crawler_list.html'  前台的html模板,下个章节可以看下模板里面的内容具体是什么样的,主要就是html和css这些前端代码。

    context_object_name = 'crawler_list' 对象名,用于和模板中的对象对应,自动填充后端的数据到前端的模板中。

def get_queryset(self)  数据查询的具体实现逻辑,里面的keyword ... Link_dns等字段也需要和模板中的对应。

 

8.2.3 模板

Templates目录主要用于保存前端模板文件。

这里主要看一下上面用到的testweb/crawler_list.html模板:

<table class="imagetable"  border="1" width="100%">
    <tr>
        <th>关键字</th>
        <th>主机IP</th>
        <th>归属地</th>
        <th>主域名</th>
        <th>关联域名</th>
        <th>详情</th>
    </tr>

{% for crawler in crawler_list %}
    <tr>
        <td>{{ crawler.keyword }}</td>
        <td>{{ crawler.host_ip }}</td>
        <td>{{ crawler.belong }}</td>
        <td>{{ crawler.main_dns }}</td>
        <td>{{ crawler.link_dns }}</td>
        <td><a href="/crawler/detail/?crawler_id={{ crawler.id }}">详情</a></td>
    </tr>

{% endfor %}
</table>

 

解释 :

<table> ... </table>是一个数据表。

<tr> .. 关键字 ....  </tr>  第一列表头

{% for crawler in crawler_list %} 这个crawler_list和视图view.py中context_object_name = 'crawler_list'对应,视图的返回值作为crawler_list变量。

<td>{{ crawler.keyword }}</td> 字段的就是crawler_list的字段。

 

到这里, 应该就能整体串起来了。

注册url路由,路由关联view视图。

View实现后台数据操作,并且关联模板,将数据返回填充到模板对应的地方。

 

到这里,django的一个小项目就实现了,可以基于这个源码,来改造一个XXX管理系统,XXX博客系统什么的。

后台主要来介绍纯后台的模块,例如celery、redies等开源组件的移植和使用。

 

这里,提供一个自动化安装脚本,在centos上一键安装即可使用。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xiaxiadeng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值