Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初学者,有一个疑问,希望能细心解答 #22

Open
yuzeshan opened this issue Oct 24, 2014 · 8 comments
Open

初学者,有一个疑问,希望能细心解答 #22

yuzeshan opened this issue Oct 24, 2014 · 8 comments

Comments

@yuzeshan
Copy link

1、编写数据模型时,@models.permalink def get_absolute_url(self): return ('post', (), {'pk': self.pk})定义这个函数是什么意思呢?
2、views中post = get_object_or_404(Post, pk=pk)这个怎么能获取数据库中指定的内容对象?pk是什么呢?
3、编写urls时,?P\d+)正则表达式又是什么意思呢?
问题比较多,请原谅我这个菜鸟。

@wwq0327
Copy link
Owner

wwq0327 commented Oct 25, 2014

1.如果不定义这个函数,我们在模版中访问一个单元条记录的时候,哪链接是这样的一条记录:/post/1,在模版中,我们只能拿到记录的ID,于是我们会这样写:/post/{{ post.id}},但当我们需要修改下url的时候,比如要改成/p/1,这时我们就需要中到各个模版中去修改,但如果使用了1中的函数之后,我们只要在编写的时候使用了这样的代码:post.get_absolute_url,那么需要修改链接之时,只需在urls中修改即可,而不需要跑到各个模版中修改代码。

2.这个问题,你可以看下这里:

http://zhangfortune.iteye.com/blog/2124979

3.这个其实就是一个正则分组,指定名称之后,你在编写相应的views的时候,就可以关联上指定的这个组名了。

希望能你有所帮助。

如果初学Django,建议可以先看看Django Book,中英文都有,有个大致认识之后再试着做些小项目。

@yuzeshan
Copy link
Author

哦,非常感谢你的细致回答,明白了a

@n37r06u3
Copy link

get_absolute_url 这个函数名字应该是约定的吧 可以写短的名字

pk是取已经有的字段的主键 可以用id代替(如果id是默认主键的话),不过有多个主键就不知道怎么样弄了

@wwq0327
Copy link
Owner

wwq0327 commented Oct 27, 2014

@yuzeshan 客气

@n37r06u3 确实是一个自定义的函数。但这种命名方法使用其意义非常名确。

@yuzeshan
Copy link
Author

@wwq0327 ,@n37r06u3再问一个问题啊!你们都是用什么来部署Django项目的?Apache+mod_wsgi还是nginx+uwsgi?

@wwq0327
Copy link
Owner

wwq0327 commented Oct 28, 2014

@yuzeshan 我使用的是Nginx+FastCGI

@yuzeshan
Copy link
Author

@wwq0327 有比较细致的部署教程吗?给我发份过来呗!网上鱼龙混杂的,谢啦!

@n37r06u3
Copy link

请用 nginx + gunicorn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants