the5fire / django-practice-book Goto Github PK
View Code? Open in Web Editor NEW《Django企业开发实战》已出版
Home Page: http://django-practice-book.com/?fr=github
《Django企业开发实战》已出版
Home Page: http://django-practice-book.com/?fr=github
安装xadmin插件失败,完成到221页,启动程序报如下错:
Traceback (most recent call last):
File "manage.py", line 26, in
execute_from_command_line(sys.argv)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/management/init.py", line 363, in execute_from_command_line
utility.execute()
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/management/init.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/management/base.py", line 327, in execute
self.check()
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/utils/functional.py", line 35, in get
res = instance.dict[self.name] = self.func(instance)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/utils/functional.py", line 35, in get
res = instance.dict[self.name] = self.func(instance)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/home/felix/.virtualenvs/typeidea-env/lib/python3.6/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/home/felix/workspace/typeidea/typeidea/urls.py", line 99, in
url(r'^xadmin/', xadmin.site.urls),
File "/home/felix/workspace/typeidea/xadmin/sites.py", line 339, in urls
return self.get_urls(), self.name, self.app_name
File "/home/felix/workspace/typeidea/xadmin/sites.py", line 330, in get_urls
for path, clz, name in self._registry_modelviews
File "/home/felix/workspace/typeidea/xadmin/sites.py", line 330, in
for path, clz, name in self._registry_modelviews
File "/home/felix/workspace/typeidea/xadmin/sites.py", line 289, in create_model_admin_view
return self.get_view_class(admin_view_class, option_class).as_view()
File "/home/felix/workspace/typeidea/xadmin/sites.py", line 281, in get_view_class
dict({'plugin_classes': plugins, 'admin_site': self}, **opts))
File "/home/felix/workspace/typeidea/xadmin/sites.py", line 29, in new
return type.new(cls, str(name), bases, attrs)
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
在github下载了本书第10章代码的分支typeidea-book-10-xadmin-and-other,运行后发现以下问题:
1.在管理后台添加分类时,会要求必须填入文章标题,填写文章标题并保存,页面出现提示“请修正下面的错误。”,但却没有显示错误出现在哪个字段上,导致无法保存成功;尽管可以点击文章所在的区域的“x”按钮关闭文章字段从而保存分类成功, 但这个页面上的关联逻辑从业务角度讲是不合理的,会对使用者造成困扰。
接着,激活虚拟环境
$ source bin/activite
应为
$ source bin/activate
勘误:5.4.2节,第103页
第二行代码少了缩进:
try:
post = Post.objects.get(id=1)
# 其余代码省略
应改为:
try:
post = Post.objects.get(id=1)
# 其余代码省略
pip install django-rest-framework
应该是
pip install djangorestframework
第一段代码上面的文字:
我们通过 Form 来定制 status 这个字段的展示:
此处笔误,与下面代码对照,改的是 desc
字段而不是 status
,所以应改为:
我们通过 Form 来定制 desc 这个字段的展示:
socket_server.py
这个代码在anaconda上直接报错了,能够看下吗?
File "d:\python_project\django-practice-book\code\chapter2\section2\socket_server.py", line 15, in
response = b'\r\n'.join(response_params)
TypeError: sequence item 0: expected a bytes-like object, str found
还有这个fork_server.py也报错,能够看下吗
pycd : 无法将“pycd”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 1
+ CategoryInfo : ObjectNotFound: (pycd:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
http://127.0.0.1:8000
Traceback (most recent call last):
File "c:\Users\pengx.vscode\extensions\ms-python.python-2019.1.0\pythonFiles\ptvsd_launcher.py", line 45, in
main(ptvsdArgs)
File "c:\Users\pengx.vscode\extensions\ms-python.python-2019.1.0\pythonFiles\lib\python\ptvsd_main_.py", line 348, in main
run()
File "c:\Users\pengx.vscode\extensions\ms-python.python-2019.1.0\pythonFiles\lib\python\ptvsd_main_.py", line 253, in run_file
runpy.run_path(target, run_name='main')
File "D:\ProgramData\Anaconda3\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "D:\ProgramData\Anaconda3\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "D:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "d:\python_project\django-practice-book\code\chapter2\section2\fork_server.py", line 60, in
main()
File "d:\python_project\django-practice-book\code\chapter2\section2\fork_server.py", line 49, in main
conn, address = serversocket.accept()
File "D:\ProgramData\Anaconda3\lib\socket.py", line 212, in accept
fd, addr = self._accept()
BlockingIOError: [WinError 10035] 无法立即完成一个非阻止性套接字操作。
怎么一开始示例代码就不能运行啊
倒数第 6 行的 SQL
语句:
SELECT * FROM post WHERE title ILIKE '%<keyword>%' or title ILIKE '%<keyword>%'
第二个 title
应为 desc
,应改为:
SELECT * FROM post WHERE title ILIKE '%<keyword>%' or desc ILIKE '%<keyword>%'
http://django-practice-book.com/ 老师您好,这个博客的源代码有吗?想学习一下,简洁大方。
106页,F表达式里,第二个代码块缩进有问题,跟其他地方的缩进风格不一致
from django.db.models import F
post = Post.objects.get(id=1)
post.pv = F('pv') + 1
post.save()
似乎用下面的方式比较好:
from django.db.models import F
post = Post.objects.get(id=1)
post.pv = F('pv') + 1
post.save()
上属第6行
<news:news>
{% if url.item.created_time %}
<news:publication_date>{{ url.item.create_time|date:"Y-m-d" }}</news:publication_date>
{% endif %}
{% if url.item.tags %}
<news:keywords>{{ url.item.tags }}</news:keywords>
{% endif %}
</news:news>
create_time 应该为created_time
try:
tag = Tag.objects.get(id=tag_id)
except Tag.DoesNotExist:
post_list = []
else:
post_list = tag.post_set.filter(status=Post.STATUS_NORMAL)
这里的tag.post_set
是什么作用? post_set
是什么语法?
书中示例代码指出文件位置是/code/chapter2/section2/thread_socketserver.py,但目前没看着该文件,请添加。
用于判断用户是否登录的
应改为
is_authenticated是一个属性而不是一个方法。
【注】django-2.0,不同版本不知道是否存在差异
作者,您好,很有幸拜读您的大作,在边看边学习的过程中,发现以下两个问题:
1、在python3.81和DJang3.3环境中,10.3.5中blog/adminforms.py页 dal模块中的两个方法 MoelSelect2()和MoelSelect2Multiple()总是会被报错,导致程序不能运行。
2、大作中所介绍的第三方管理后台 xadmin在python3.81和DJang3.3环境中报错太多,无法使用。
9.3.5 代码倒数第3行,缺少
install_requires 中的依赖包:
django-rest-framework==0.1.0
应该更正为:
djangorestframework==3.8.2
图1-1,SideBar中,created_time的字段类型不应该为str,应该为datetime类型!
本书中所有这张关系图中的SideBar的created_time字段类型都是错的!
line 21: while EOL1 not in request and EOL2 not in request:
TypeError: a bytes-like object is required, not 'str'
在设置view_name='api-post-detail'时报错
Request Method: | GET |
---|---|
http://127.0.0.1:8000/api/post/?offset=2 | |
1.11.10 | |
ImproperlyConfigured | |
Could not resolve URL for hyperlinked relationship using view name "api-post-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field. |
|
C:\ProgramData\Anaconda3\envs\dBlog\lib\site-packages\rest_framework\relations.py in to_representation, line 391 | |
C:\ProgramData\Anaconda3\envs\dBlog\python.exe | |
3.6.8 |
修改post_list和post_detail时,应该先在views.py中导入SideBar包:
from config.models import SideBar
按照3.4.3章节提示,在test.py 中添加了StudentTestCase类和各个函数的代码。
可是并没有找到如何进行测试的方法,请问我该如何进行测试。
我的环境是: Mac + python3.6 + django2.1.7 + PyCharm2018.3
https://github.com/the5fire/django-practice-book/blob/master/chapter1/section3.md
导航只是分类,默认展示在顶部。同时每篇文章都需要有面包屑,以告知读者目前所处 问题,面包屑组成如下:首页>文章所属分类> 正文。导航的顺序作者可以进行设置权重,权重高者在前。顶部最多展示6个分类,多余的分类展示到底部。
逛到前辈的github,打算把md和源码都过一遍hhh,我自己也有写过一个Django-blog。地址是:https://github.com/enjoy-binbin/Django-blog
query = cls.objects.filter(status=cls.STATUS_NORMAL)
改为
return cls.objects.filter(status=cls.STATUS_NORMAL)
setting的拆分, 项目运行没有问题,但是在pychram中,打开python console后,会出现以下错误:
Traceback (most recent call last):
File "", line 6, in
File "D:\myProject\typeidea\lib\site-packages\django_init_.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "D:\myProject\typeidea\lib\site-packages\django\conf_init_.py", line 79, in getattr
self.setup(name)
File "D:\myProject\typeidea\lib\site-packages\django\conf_init.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
以下是我manage.py的代码:
import os
import sys
def main():
profile = os.environ.get('TYPEIDEA_PROFILE', 'develop')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', f'typeidea.settings.{profile}')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if name == 'main':
main()
看了一遍目录,好像没有找到i8n和l10n, 还望补课
为啥非要非阻塞+多线程,但是我阻塞监听+多线程也可以完成多人同时访问啊,
倒数第5行:
modles.py --> models.py
第四章和第五章只有纸质版才有么?
version : Django==1.11.29
原文描述:
执行完上述方法, 并且Django帮我们执行完view, 拿到最终的 response 后, 如果使用了模板的 response (这里指通过 return render(request, 'index.html', context={}) 方式返回的 response), 就会来到这个方法中.
实际测试后发现:
def index(request):
students = Student.get_all()
form = StudentForm()
context = {
"students": students,
"form": form,
}
return render(request, 'index.html', context=context)
# return TemplateResponse(request, "index.html", context=context)
process_response
方法;return TemplateResponse(request, "index.html", context=context)
而非 return render(request, 'index.html', context=context)
才会进入 process_template_response
方法中这是我的发现, 欢迎大家指出我过程中是否存在问题导致和 书籍 中 描述的结论不符.
按照书上提示 , 将 css 与 js 引入到自定义的 class Media 中
代码片段如下:
class Media:
css = {
'all' : ('https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css'),
}
js = ('https://cdn.bootcss.com/bootstrap/4.0.0-beta.2/js/bootstrap.bundle.js',)
当运行 runserver 后, 页面无任何变化 , 提示框里提示很多
"GET /static/.* HTTP/1.1" 404 1616
的字样(.*表示出现的各种字符统称)
请问是否正确?
对于已经参考本书完成个人博客开发和部署的,欢迎到这个下面留言,格式如下:
博客地址:https://www.the5fire.com
源码地址:https://github.com/the5fire/typeidea
开发心得:xxxx
10.1.4 节第一段话出现了三次 crispy_forms
,后面两个写成了 cripy_forms
,应改为 crispy_forms
。
之前第5章的例子里 Post类正文的属性是content
但是到了11章 237 页 11.1.1 和 243页 11.2 的代码里就变成了content_html
了
urlpatterns = [
#配置后台页面
url(r'^super_admin/', super_site.urls, name='super-site'),
url(r'^custom_admin/', custom_site.urls, name='custom-site'),
url(r'^admin/', admin.site.urls, name='admin'),
#配置用户页面
url(r'^$', IndexView.as_view(), name='index'),
url(r'^category/(?P<category_id>\d+)/$', CategoryView.as_view(),
name='category-list'),
url(r'^tag/(?P<tag_id>\d+)/$', TagView.as_view(),
name='tag-list'),
url(r'^post/(?P<post_id>\d+).html$', PostDetailView.as_view(),
name='post-detail'),
url(r'^links/$', links, name='links')
]
其中的 url(r'^post/(?P<post_id>\d+).html$', PostDetailView.as_view(), name='post-detail'),
在html$
后面添加/
,最后改为html/$
注意 框中
原句:读者可以尝试把test.py文件中的 student.sex_show 改为student.get_sex_display试试。
修改:student.get_sex_display 需要加 括号,改为 student.get_sex_display() 。
第 11 行代码:
<form class="form-inline my-2 my-lg-0" action='GET'>
中的 action
应改为 method
。
CommentView的post方法中,关于评论的提交,如果评论有效,会直接跳转到评论的来源页(target)。
如果这样,下方模板页中“评论成功!”的部分,是不需要的。
在引入comment_blocks发生了报错
后在setting的options里加入了
'libraries':{
'comment_block': 'comment.templatetags.comment_block',
},
得以解决
list_filter = ['category'] 错误
应该为:
list_filter = ['category__name']
6.3
节下面的第二段文字,原文为“重写了 save
方法”,应更正为“重写了 save_model
方法”。
@classmethod
def latest_posts(cls):
queryset = cls.objects.filter(status=cls.STATUS_NORMAL)
return queryset
代码部分第 7
行,原文为:
list_display_links = []
根据上下文,此处作者的用意应该是禁用位于列表页每行行首的超链接,这个链接可以跳转到编辑页面,但是这样写没有效果,应当改成:
list_display_links = None
这是我的个人理解,不一定正确,错了请包涵。
出错位置P179页中的
此处的cate对象是不存在的,
浏览器访问,会报错为:
Exception Type: | NoReverseMatch |
---|---|
Reverse for 'category-list' with arguments '('',)' not found. 1 pattern(s) tried: ['category/(?P<category_id>\d+)/$'] |
将{% url 'category-list' cate.id %}">,修改为{% url 'category-list' post.category_id %}">
正常访问
179页 基于bootstrap美化页面
list.html 页面
分类的url,cate.id 需要改为 post.id
原文代码:
fieldsets = (
(None, {
'fields': (
'name',
('sex', 'profession'),
('email', 'qq', 'phone'),
'status',
)
}),
)
应修改为
fieldsets = [
(None, {
'fields': (
'name',
('sex', 'profession'),
('email', 'qq', 'phone'),
'status',
)
})
]
不然会直接报错误:
(admin.E008) The value of 'fieldsets[0]' must be a list or tuple
书中对于第五章django 的setting的拆分 缺少了一步,拆分后项目无法正常运行的
需要在settigns/init.py文件中加入from.base import *
from .develop import *
才能正常
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.