"canonical" views.py in Django Unleashed book, Chapter 22

72 views
Skip to first unread message

Malik Rumi

unread,
Jun 29, 2016, 8:13:42 PM6/29/16
to Django users
Although I love the book (so far) for its level of detail and addressing a LOT of issues I am currently dealing with, I have run across a problem. Specifically, the views.py and urls.py in the github repo are NOT the same as the snippets of views.py and urls.py in the book. Now, since I started in Chapter 20, because that's where my issues are, it is possible that all of this is explained and cleared up elsewhere, but a quick search in the online version of the book did not reveal same to me, so,as the author himself asks, I am posting here.

Github views.py

starting at line 128:
@class_login_required
class ProfileDetail(
ProfileGetObjectMixin, DetailView):
model = Profile


views.py book example 22.13
 26   from core.utils import UpdateView
...
134   @class_login_required
135   class ProfileUpdate(
136           ProfileGetObjectMixin, UpdateView)
:137       fields = ('about',)
138       model = Profile

There is no core.utils or UpdateView in the github version


views.py in book example 22.19
134   class PublicProfileDetail(DetailView):
135       model = Profile

There is no PublicProfileDetail in the github version


urls.py in book example 22.20
 10   from .views import (
11       ActivateAccount, CreateAccount,
12       DisableAccount, ProfileDetail, ProfileUpdate,
13       PublicProfileDetail, ResendActivationEmail)  .      
...
67   urlpatterns = [  .     
 ...
114       url(r'^(?P<slug>[\w\-]+)/$',
115           PublicProfileDetail.as_view(),
116           name='public_profile'),
117   ]

Again, there is no PublicProfileDetail in the github version, which stops at line 111

There may be more, but that's what I have right now. It does not make sense to me that the author would go to all this trouble to talk about Public Profiles and then omit them, especially since allowing users to both have them and edit them themselves makes so much sense. Now, I do have the code from the book, which is why I framed my question as the "canonical" version of these codes. Thanks.

Andrew Pinkham

unread,
Jun 30, 2016, 9:37:35 PM6/30/16
to django...@googlegroups.com, malik....@gmail.com
Hi Malik!
Thanks for buying my book! I'm glad to hear you like Django Unleashed, but I'm sorry you're frustrated with the github repo at https://github.com/jambonrose/DjangoUnleashed-1.8 .

Page xiv provides a quick explanation of git commits' relationship to the examples in the book. In short: each example in the book has a corresponding commit in the github repo (1+ examples per commit). This is why each example lists 'filename in hash' at the top of the example. If you suffix http://dju.link/ with a commit, it will take you to the diff on github.

For instance, in example 22.13 (which you referenced), the example is listed as being 'user/views.py in 83167965e8'. You can browse to http://dju.link/83167965e8 to be redirected to the diff of that commit on github, or else to http://dju.link/83167965e8/user/views.py to see the state of the file at that commit.

NB: if you browse the code on github without adjusting the commit, you're seeing the code as it is in Chapter 29 (Chapter 30 has no code in the repo).

If you don't want to browse github, you can also clone the repo, checkout commit 83167965e8, and then have the project to run directly on your computer!

Note that each commit message is prefixed with the Chapter that references it (such as 'Ch22: Create Profile Update web page.'). What's more, the sample gitconfig file in the repo comes with a few neat commands to make moving around the repo easier. See https://github.com/jambonrose/DjangoUnleashed-1.8#walking-the-repository for more info.

Now, to address some of your points directly:

> There is no core.utils or UpdateView in the github version

https://github.com/jambonrose/DjangoUnleashed-1.8/blob/master/core/utils.py#L5

> There is no PublicProfileDetail in the github version

http://dju.link/a1d420b17a
->
https://github.com/jambonrose/DjangoUnleashed-1.8/commit/a1d420b17a/

You can then browse to:
https://github.com/jambonrose/DjangoUnleashed-1.8/blob/a1d420b17a/user/views.py#L134

> Again, there is no PublicProfileDetail in the github version, which stops at line 111

http://dju.link/a1d420b17a/user/urls.py
->
https://github.com/jambonrose/DjangoUnleashed-1.8/blob/a1d420b17a/user/urls.py

Relevant line is 114:
https://github.com/jambonrose/DjangoUnleashed-1.8/blob/a1d420b17a/user/urls.py#L114

I'm not super sure what you were looking at, or where the confusion occurred, but I would be open to your feedback about what you were seeing and how you got there, so that I may help others better get the code examples (as I spent *a lot* of time making the repo as cleans as possible given my deadlines).

Hope that's helpful,
Andrew

Malik Rumi

unread,
Jun 30, 2016, 10:42:52 PM6/30/16
to Django users
1. I don't know if it makes any difference, but I don't have the physical book. I am reading it online thru my safarionline subscription.
2. Once I got to your repo, I just clicked around to go to views or urls or whatever. I didn't think of looking for different commits, because I assumed, wrongly, that there would only be one, final, 'canonical' version of the code.
3. Looking specifically at urls.py on githhub, I can now see that the link you pointed me to is at a different url and has a different commit than the one I had looked at. Compare

https://github.com/jambonrose/DjangoUnleashed-1.8/blob/a1d420b17a/user/urls.py
https://github.com/jambonrose/DjangoUnleashed-1.8/blob/83167965e86b82a86a25504c82285dccd79b9e32/user/urls.py

So that must be the issue. Not sure what you can do about that except put a lot of notes in the repo. Cloning the whole thing is probably a good idea.

For your marketing fyi, I found out about you and the book after looking at your DjangoCon talk about the request response cycle. It so happens I have an immediate need to swap out the default user, so those chapters were right on time for me.

Thanks.

Malik Rumi

unread,
Jun 30, 2016, 11:00:32 PM6/30/16
to Django users
p.s. -

I did have a couple of other questions, if you don't mind:

1. You end 22.6 by saying

In an actual website, you likely won’t interact with the auth in the way we have. You’ll rely on third-party apps to provide object-level permissions or else on authentication with other services such as Twitter and Facebook.

I found that surprising, but then I'm somewhat isolated so don't claim to know what other folks are doing. Why is this the common practice? Just to save time because the tools are available?

2. In 22.5, you recommended a static url for profile pages.  Quite apart from how often people may update their pages, I didn't understand why you suggest that for an otherwise dynamic website. I guess I just assumed static urls and static (or 'flat'. as Django likes to call them) pages go together, like About Us, and that such pages would be the exception. Was this just to make the profiles easy to find and/or bookmark for end users?

Thanks again.




On Wednesday, June 29, 2016 at 5:13:42 PM UTC-7, Malik Rumi wrote:

13515...@qq.com

unread,
Jul 1, 2016, 2:06:17 AM7/1/16
to django-users
hi everyone:

i defined tbls  customer and salesorder in my db, 
i want to show the customer name in my form but want not to save it in salesorder tbl,
how can i initial it for gui?
my code is :
#model
class Customer(models.Model):
name=models.CharField(max_length=40)
abbreviation=models.CharField(max_length=20)

class SalesOrder(models.Model):
customer=models.ForeignKey(Customer,related_name='by_customer',on_delete=models.CASCADE)

#form
class SalesOrderForm(forms.ModelForm):
customer_name=forms.CharField(required=False,widget=forms.TextInput(attrs={'readonly':''}))
class Meta:
model=SalesOrder
fields = ['customer','customer_name',]

thanks


 

13515...@qq.com

unread,
Jul 15, 2016, 6:48:48 AM7/15/16
to django-users
hi everyone:

I am tring to deploy my site by nginx and uwagi
in my project yaohang,i have these files:

#yaohang_uwsgi.ini
[uwsgi] 
socket = :8001 
chmod-socket    = 664
master = true 
module = yaohang_uwsgi 
processes = 8 
listen = 120 
enable-threads = true 
daemonize = /home/wangwei/yaohang/yaohang_uwsgi.log 
pidfile = /home/wangwei/yaohang/yaohang_uwsgi.pid 
pythonpath = /home/wangwei/yaohang/ 
pythonpath = /home/wangwei/yaohang/yaohang_uwsgi 
pythonpath = /home/wangwei/yaohang/yaohang 
buffer-size = 32768 
reload-mercy = 8 
vacuum = true

#yaohang_uwsgi.py
import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yaohang.settings") 

from django.core.wsgi import get_wsgi_application 
application = get_wsgi_application()

#yaohang_uwsgi.conf
server { 
listen      8000;
server_name    192.168.70.32; # this is my server's addr
charset UTF-8; 

client_max_body_size 75M; 

location / { 
include uwsgi_params; 
uwsgi_pass 127.0.0.1:8001
uwsgi_read_timeout 2; 
}    
location /static { 
expires 30d; 
autoindex on; 
add_header Cache-Control private; 
alias /home/wangwei/yaohang/static/; 

}   

run nginx and 
>>uwsgi --http 192.168.70.32:8001 --wsgi-file yaohang_uwsgi.py

and then by my client browser:192.168.70.32:8000
it show:502 Bad Gateway

and with the message in file /var/log/nginx/error.log :
connect() to unix:///home/wangwei/yaohang/yaohang.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.71.3, server: 192.168.70.32, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///home/wangwei/yaohang/yaohang.sock:", host: "192.168.70.32:8000"

who can tell me where is wrong?

thank you!

wblueboat
 

Rafael E. Ferrero

unread,
Jul 15, 2016, 7:20:15 AM7/15/16
to django...@googlegroups.com


Rafael E. Ferrero

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/201607151847061918789%40qq.com.
For more options, visit https://groups.google.com/d/optout.

Javier Guerra Giraldez

unread,
Jul 15, 2016, 8:14:41 AM7/15/16
to django...@googlegroups.com
On 15 July 2016 at 11:47, 13515...@qq.com <13515...@qq.com> wrote:
> and with the message in file /var/log/nginx/error.log :
> connect() to unix:///home/wangwei/yaohang/yaohang.sock failed (13:
> Permission denied


check under what user is running nginx, and make sure that it has r/w
permissions on that socket

--
Javier

Andrew Pinkham

unread,
Oct 23, 2016, 11:09:38 AM10/23/16
to django...@googlegroups.com
Hi Malik,

On Jun 30, 2016, at 11:00 PM, Malik Rumi <malik....@gmail.com> wrote:
> I found that surprising, but then I'm somewhat isolated so don't claim to know what other folks are doing. Why is this the common practice? Just to save time because the tools are available?

Yup - this is a general software practice: Why reinvent the wheel when it's available to you?

Andrew
http://jambonsw.com
http://django-unleashed.com

Reply all
Reply to author
Forward
0 new messages