Ensure an object is saved even when the atomic bock rollsback

2015-05-08 Thread Marc Aymerich
HI,

I'm using atomic requests, but one of my views needs to save a model
regardless of wheter the current transaction rolls back or not.

I'm confused about how to proceed with the current autocommit behaviour. Do
I need to use a different db connection? or perhaps there is some way of
telling django to ignore autocommit for some particular block ?

Thanks!
-- 
Marc

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BDCN_uB1GeYJyOfKVbwiu8tG2ECjhQCgqdtdMXa4tCZNwVtHg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Ensure an object is saved even when the atomic bock rollsback

2015-05-08 Thread Carl Meyer
Hi Marc,

On 05/08/2015 07:15 AM, Marc Aymerich wrote:
> I'm using atomic requests, but one of my views needs to save a model
> regardless of wheter the current transaction rolls back or not.
> 
> I'm confused about how to proceed with the current autocommit behaviour.
> Do I need to use a different db connection? or perhaps there is some way
> of telling django to ignore autocommit for some particular block ? 

You'd need to use a different db connection, since what you're trying to
do violates the very nature of a database transaction. If you were just
trying to run some raw SQL, you could establish the separate connection
yourself manually, but if you're trying to save a Django model, you'll
probably need a second connection defined in your DATABASES setting.

Possible alternatives:

- If you're using a task queuing system (like Celery or rq), queue a
task to perform the save; you can do this without making it conditional
on the transaction committing, as long as your task queue is using a
store other than your primary database (e.g. Redis or RabbitMQ). Usually
when people do this it's unintentional and a bug (they really wanted the
task to execute only if the current transaction committed), but in your
case it could be a feature.

- Switch from ATOMIC_REQUESTS to explicit use of transaction.atomic() in
your views, so that you can place this "no matter what" save in its own
transaction.

Carl

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/554CDB37.7060503%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature


Generic template; determining information about calling view/app

2015-05-08 Thread David
Hi

Sorry if this is confusing, it's the best I can describe.

I have been separating my templates for inheritance clarity. I have a 
generic template 'category_list.html'. This can be used by different apps 
because the layout is the same.

What I need to do in order to use appropriate links in the template is to 
find out:

a) the name of the application calling it
b) the namespace of the application url so I can use a dynamic {% url %} tag

Firstly, I guess, is this possible?

Yes, I can inherit the templates further and pass info directly from the 
views, but the creation of extra templates just for these 2 things seemed 
like I was doing something wrong.

Many thanks for any assistance.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/64637401-2cbd-4f8c-b7c0-4910aeceeb84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Ensure an object is saved even when the atomic bock rollsback

2015-05-08 Thread Marc Aymerich
On Fri, May 8, 2015 at 5:50 PM, Carl Meyer  wrote:

> Hi Marc,
>
> On 05/08/2015 07:15 AM, Marc Aymerich wrote:
> > I'm using atomic requests, but one of my views needs to save a model
> > regardless of wheter the current transaction rolls back or not.
> >
> > I'm confused about how to proceed with the current autocommit behaviour.
> > Do I need to use a different db connection? or perhaps there is some way
> > of telling django to ignore autocommit for some particular block ?
>
> You'd need to use a different db connection, since what you're trying to
> do violates the very nature of a database transaction. If you were just
> trying to run some raw SQL, you could establish the separate connection
> yourself manually, but if you're trying to save a Django model, you'll
> probably need a second connection defined in your DATABASES setting.
>
> Possible alternatives:
>
> - If you're using a task queuing system (like Celery or rq), queue a
> task to perform the save; you can do this without making it conditional
> on the transaction committing, as long as your task queue is using a
> store other than your primary database (e.g. Redis or RabbitMQ). Usually
> when people do this it's unintentional and a bug (they really wanted the
> task to execute only if the current transaction committed), but in your
> case it could be a feature.
>

Yep, the thing is that I need to have this instance saved before handling
it to a worker process, the worker process uses it and if something goes
wrong on the main thread then the worker has an object that does not exists
on the database. Now I realize that I can just catch the IntegrityError and
re-save the object on the worker thread ;)

obj.pk = None
obj.save()


- Switch from ATOMIC_REQUESTS to explicit use of transaction.atomic() in
> your views, so that you can place this "no matter what" save in its own
> transaction.


I've been doing something on those lines, but now I have this corner case
which I have surouding code that needs to be wrapped up in a transaction :(

For the record, after exploring django.db I realize of 2 more alternative
ways of dealing with this situation:

1) because the connection pool is a local thread object, you can just
fireup a separate thread, so it will have fresh connections :)

t = threading.Thread(target=backend.create_log, args=args)
t.start()
log = t.join()

2) because a new connection is created for each settings.DATABASES, it is
easy to have new connections with a fake database. I've also created a
context manager for that

from django import db
from django.conf import settings as djsettings

class fresh_connection(object):
def __init__(self, origin, target):
self.origin = origin
self.target = target

def __enter__(self):
djsettings.DATABASES[self.target] =
djsettings.DATABASES[self.origin]
# Because db.connections.datases is a cached property
db.connections = db.utils.ConnectionHandler()

def __exit__(self, type, value, traceback):
db.connections[self.target].close()
djsettings.DATABASES.pop(self.target)


with fresh_connection('default', 'other_default'):
log = backend.create_log(*args, using='other_default')
log._state.db = 'default'


>
> Carl
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/554CDB37.7060503%40oddbird.net
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Marc

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BDCN_tc946DGihEAu1xqJOgHR8U4td%2Bb-TPS7zu8xAs1jQnnA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Ensure an object is saved even when the atomic bock rollsback

2015-05-08 Thread Marc Aymerich
On Fri, May 8, 2015 at 6:43 PM, Marc Aymerich  wrote:

>
>
> On Fri, May 8, 2015 at 5:50 PM, Carl Meyer  wrote:
>
>> Hi Marc,
>>
>> On 05/08/2015 07:15 AM, Marc Aymerich wrote:
>> > I'm using atomic requests, but one of my views needs to save a model
>> > regardless of wheter the current transaction rolls back or not.
>> >
>> > I'm confused about how to proceed with the current autocommit behaviour.
>> > Do I need to use a different db connection? or perhaps there is some way
>> > of telling django to ignore autocommit for some particular block ?
>>
>> You'd need to use a different db connection, since what you're trying to
>> do violates the very nature of a database transaction. If you were just
>> trying to run some raw SQL, you could establish the separate connection
>> yourself manually, but if you're trying to save a Django model, you'll
>> probably need a second connection defined in your DATABASES setting.
>>
>> Possible alternatives:
>>
>> - If you're using a task queuing system (like Celery or rq), queue a
>> task to perform the save; you can do this without making it conditional
>> on the transaction committing, as long as your task queue is using a
>> store other than your primary database (e.g. Redis or RabbitMQ). Usually
>> when people do this it's unintentional and a bug (they really wanted the
>> task to execute only if the current transaction committed), but in your
>> case it could be a feature.
>>
>
> Yep, the thing is that I need to have this instance saved before handling
> it to a worker process, the worker process uses it and if something goes
> wrong on the main thread then the worker has an object that does not exists
> on the database. Now I realize that I can just catch the IntegrityError and
> re-save the object on the worker thread ;)
>
> obj.pk = None
> obj.save()
>

unfortunately this will not work in my case, because I need the object.id
to not change :(


> - Switch from ATOMIC_REQUESTS to explicit use of transaction.atomic() in
>> your views, so that you can place this "no matter what" save in its own
>> transaction.
>
>
> I've been doing something on those lines, but now I have this corner case
> which I have surouding code that needs to be wrapped up in a transaction :(
>
> For the record, after exploring django.db I realize of 2 more alternative
> ways of dealing with this situation:
>
> 1) because the connection pool is a local thread object, you can just
> fireup a separate thread, so it will have fresh connections :)
>
> t = threading.Thread(target=backend.create_log, args=args)
> t.start()
> log = t.join()
>
> 2) because a new connection is created for each settings.DATABASES, it is
> easy to have new connections with a fake database. I've also created a
> context manager for that
>


So my best option so far is to make that query on a separate connection,
and between worker threads (preferable than celery in this case) and moking
the database settings... the later is hacky but much faster :)

>>> from threading import Thread
>>> from django import db
>>> from django.conf import settings as djsettings
>>> class fresh_connection(object):
... def __init__(self, origin, target):
... self.origin = origin
... self.target = target
...
... def __enter__(self):
... djsettings.DATABASES[self.target] =
djsettings.DATABASES[self.origin]
... # Because db.connections.datases is a cached property
... self.old_connections = db.connections
... db.connections = db.utils.ConnectionHandler()
...
... def __exit__(self, type, value, traceback):
... db.connections[self.target].close()
... djsettings.DATABASES.pop(self.target)
... db.connections = self.old_connections
...
>>> def do_query(using='default'):
... Account.objects.using(using).get(pk=1)
...
>>> from datetime import datetime
>>> now = datetime.now()
>>> for i in range(1, 100):
... t = Thread(target=do_query)
... t.start()
... t.join()
...
>>> print((datetime.now()-now).total_seconds())
1.449795
>>>
>>> now = datetime.now()
>>> for i in range(1, 100):
... with fresh_connection('default', 'other_default'):
... do_query(using='other_default')
...
>>> print((datetime.now()-now).total_seconds())
0.106981



-- 
Marc

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2BDCN_vTQqiLBym4-9MLvED2NpfWzm1F%2Bdxh_vkm3TgXEL68KA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Ensure an object is saved even when the atomic bock rollsback

2015-05-08 Thread Erik Cederstrand

> Den 08/05/2015 kl. 15.15 skrev Marc Aymerich :
> 
> HI, 
> 
> I'm using atomic requests, but one of my views needs to save a model 
> regardless of wheter the current transaction rolls back or not.
> 
> I'm confused about how to proceed with the current autocommit behaviour. Do I 
> need to use a different db connection? or perhaps there is some way of 
> telling django to ignore autocommit for some particular block ? 

You need to control your transactions explicitly. Have a look at the docs: 
https://docs.djangoproject.com/en/1.8/topics/db/transactions/#controlling-transactions-explicitly
 Especially the third example code in that paragraph shows how to commit some 
queries while rolling back others.

Erik

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/93D79E32-B433-4AE8-96B7-90E839982321%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.


Re: Django code of conduct - last line

2015-05-08 Thread Graham Oliver
Thank you, so how about

'Don’t forget that it is human to err and blaming each other doesn’t get us 
anywhere.
Instead, focus on helping to resolve issues and learning from mistakes'

?
g


On Friday, 8 May 2015 15:01:23 UTC+12, Lachlan Musicman wrote:
>
> To my mind it means something along the lines of:
>
> if someone says something sexist, instead of jumping on them and 
> eviscerating them publicly, we take them aside and explain that that type 
> of language is against the CoC, is inappropriate, and ask them to not do it 
> again. Even so far as to offer supplementary  readings/texts/opportunities 
> to discuss.
>
> Then, if the behaviour is repeated in a non accidental manner 
> (reconstruction is hard, mistakes are made!), we probably warn or ask them 
> to leave the space.
>
> That would be my reading.
>
> cheers
> L.
>
> --
> let's build quiet armies friends, let's march on their glass 
> towers...let's build fallen cathedrals and make impractical plans
>
> - GYBE
>
> On 8 May 2015 at 12:51, Graham Oliver > 
> wrote:
>
>> Hello all
>> Just reading the code of conduct https://www.djangoproject.com/conduct/
>> The last line goes like this
>>
>> 'Don’t forget that it is human to err and blaming each other doesn’t get 
>> us anywhere, rather offer to help resolving issues and to help learn from 
>> mistakes.'
>>
>> I don't really understand the 'rather offer to help resolving issues and 
>> to help learn from mistakes.'
>>
>> Anyone got a view on what this means and/or a better way to express it?
>>
>> Cheers
>> g
>>
>> -- 
>> 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 http://groups.google.com/group/django-users.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/31e23634-80d0-4d5e-8a26-8cbc99d12a71%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/8da717ba-9d17-47ae-91ca-896e76627f33%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django code of conduct - last line

2015-05-08 Thread Carl Meyer
Hi Graham,

On 05/08/2015 01:43 PM, Graham Oliver wrote:
> Thank you, so how about
> 
> 'Don’t forget that it is human to err and blaming each other doesn’t get
> us anywhere.
> Instead, focus on helping to resolve issues and learning from mistakes'

I think this is an improvement in clarity and style over the current
wording. Please see https://www.djangoproject.com/conduct/changes/ for
the process to make changes to the Code (short version: send a pull
request to http://github.com/django/djangoproject.com).

Carl

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/554D1DA9.1090401%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.


signature.asc
Description: OpenPGP digital signature


Defining base templates for reusable apps

2015-05-08 Thread Some Developer
I'm looking into building a set of open source reusable apps for Django 
and was wondering what the currently accepted best practice was for 
defining base templates for reusable apps was?


I want my reusable apps templates to be easy to slot into an already 
existing site design but I'm unsure of the best way of achieving that. 
Should I define a base template in the app itself that basically works 
on the whole of the app? If I did that what would be the best way of 
letting other people modify that base template?


I really just want my app to fit into other users projects with the 
minimum amount of fuss.


I guess I could specify the views myself and let the user create the 
templates themselves. Would that be a decent option? That would allow 
the user to design the templates in the best possible way.


Having said that I would also like to provide some default templates 
that people could use if they didn't want to bother designing the 
templates themselves. How could I allow them to override the default 
template supplied by the view?


Any hints would be greatly appreciated :).

--
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/554D23C5.4060002%40googlemail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Django code of conduct - last line

2015-05-08 Thread Graham Oliver
ok I have done that Carl
I'm new to using GitHub to collaborate so let me know if there is anything 
out of place
https://github.com/django/djangoproject.com/pull/468
Thanks for your feedback
g

On Saturday, 9 May 2015 08:34:16 UTC+12, Carl Meyer wrote:
>
> Hi Graham, 
>
> On 05/08/2015 01:43 PM, Graham Oliver wrote: 
> > Thank you, so how about 
> > 
> > 'Don’t forget that it is human to err and blaming each other doesn’t get 
> > us anywhere. 
> > Instead, focus on helping to resolve issues and learning from mistakes' 
>
> I think this is an improvement in clarity and style over the current 
> wording. Please see https://www.djangoproject.com/conduct/changes/ for 
> the process to make changes to the Code (short version: send a pull 
> request to http://github.com/django/djangoproject.com). 
>
> Carl 
>
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/c1f30ae3-9865-4d52-8530-92d209563636%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: NoReverseMatch at /polls/1/results/

2015-05-08 Thread Luis Zárate
Which urls.py you paste here? The project URLs or the app urls .?

It is because you are using namespace in the url reverse so you need to
named in your project's URLs and put the code paste here in your app urls.

El jueves, 7 de mayo de 2015, James Schneider 
escribió:
> I'm guessing the issue is actually in your template if you are following
along that page. None of the code in your views would generate that error
from what I can see.
>
> The error indicates that you are trying to reverse('polls:detail') and
not providing any arguments for the URL resolver (or are passing an
empty/missing variable). In the template, this would probably look
something like {% url 'polls:detail' %}. I would guess that you are
forgetting question.id after the view name, assuming you are using the same
variable names as the tutorial.
>
> Check near the bottom of the stack trace on the page, I bet it has a {%
url %} tag highlighted as the culprit.
>
> -James
>
> On May 7, 2015 12:09 AM, "H M"  wrote:
>>
>> I am on part 4 django tutorial. The tutorial is very good and I easily
get on part 4 but
>>
>> I get error: Reverse for 'detail' with arguments '('',)' and keyword
arguments '{}' not found. 1 pattern(s) tried:
[u'polls/(?P\\d+)/$']
>>
>> Am I missing something?
>>
>> My code is following:
>>
>>> urlpatterns = patterns('',
>>> url(r'^$', views.index, name='index'),
>>> url(r'^(?P\d+)/$', views.detail, name='detail'),
>>> url(r'^(?P\d+)/results/$',views.results,
name='results'),
>>> url(r'^(?P\d+)/vote/$', views.vote, name='vote'),
>>
>>
>> views.py:
>>
>>> from django.shortcuts import get_object_or_404, render
>>> from django.http import HttpResponseRedirect, HttpResponse
>>> from django.template import RequestContext, loader
>>> from polls.models import Question, Choice
>>> from django.http import Http404
>>> from django.core.urlresolvers import reverse
>>>
>>>
>>> def index(request):
>>> latest_question_list =  Question.objects.order_by('-pub_date')[:5]
>>> template = loader.get_template('polls/index.html')
>>> context = RequestContext(request, {'latest_question_list' :
latest_question_list, })
>>> return HttpResponse(template.render(context))
>>>
>>> def detail(request, question_id):
>>> try:
>>> question = Question.objects.get(pk = question_id)
>>> except Question.DoesNotExist:
>>> raise Http404("Question ne postoji")
>>> return render(request, 'polls/detail.html', {'question': question})
>>>
>>> def results(request, question_id):
>>> question = get_object_or_404(Question, pk = question_id)
>>> return render(request, 'polls/results.html', {'guestion': question})
>>>
>>>
>>> def vote(request, question_id):
>>> p = get_object_or_404(Question, pk=question_id)
>>> try:
>>> selected_choice  = p.choice_set.get(pk=request.POST['choice'])
>>> except (KeyError, Choice.DoesNotExist):
>>> return render(request, 'polls/detail.html', {
>>> 'question': p,
>>> 'error_message': "Nijesi izabrao pitanje.",
>>>  })
>>> else:
>>> selected_choice.votes += 1
>>> selected_choice.save()
>>> return HttpResponseRedirect(reverse('polls:results', args=(p.id
,)))
>>
>>
>>
>>
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/2e2320f1-6813-4e01-8b32-76694dbd22f8%40googlegroups.com
.
>> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUv65UQLmJKjWwySKga08gvAa0OHwkZ39hOj4CjPBS3ww%40mail.gmail.com
.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
"La utopía sirve para caminar" Fernando Birri

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAG%2B5VyP-vOtdWzW%2BCJj5kFG_XwOFYi-EJfb3Toq31fD%2BKEiGuw%40m

Re: NoReverseMatch at /polls/1/results/

2015-05-08 Thread Luis Zárate
Sorry, James is right, your problem is like James described.

Sorry for the noise I read again and see that I understood bad your problem.

Sorry

El viernes, 8 de mayo de 2015, Luis Zárate  escribió:
> Which urls.py you paste here? The project URLs or the app urls .?
>
> It is because you are using namespace in the url reverse so you need to
named in your project's URLs and put the code paste here in your app urls.
>
> El jueves, 7 de mayo de 2015, James Schneider 
escribió:
>> I'm guessing the issue is actually in your template if you are following
along that page. None of the code in your views would generate that error
from what I can see.
>>
>> The error indicates that you are trying to reverse('polls:detail') and
not providing any arguments for the URL resolver (or are passing an
empty/missing variable). In the template, this would probably look
something like {% url 'polls:detail' %}. I would guess that you are
forgetting question.id after the view name, assuming you are using the same
variable names as the tutorial.
>>
>> Check near the bottom of the stack trace on the page, I bet it has a {%
url %} tag highlighted as the culprit.
>>
>> -James
>>
>> On May 7, 2015 12:09 AM, "H M"  wrote:
>>>
>>> I am on part 4 django tutorial. The tutorial is very good and I easily
get on part 4 but
>>>
>>> I get error: Reverse for 'detail' with arguments '('',)' and keyword
arguments '{}' not found. 1 pattern(s) tried:
[u'polls/(?P\\d+)/$']
>>>
>>> Am I missing something?
>>>
>>> My code is following:
>>>
 urlpatterns = patterns('',
 url(r'^$', views.index, name='index'),
 url(r'^(?P\d+)/$', views.detail, name='detail'),
 url(r'^(?P\d+)/results/$',views.results,
name='results'),
 url(r'^(?P\d+)/vote/$', views.vote, name='vote'),
>>>
>>>
>>> views.py:
>>>
 from django.shortcuts import get_object_or_404, render
 from django.http import HttpResponseRedirect, HttpResponse
 from django.template import RequestContext, loader
 from polls.models import Question, Choice
 from django.http import Http404
 from django.core.urlresolvers import reverse


 def index(request):
 latest_question_list =  Question.objects.order_by('-pub_date')[:5]
 template = loader.get_template('polls/index.html')
 context = RequestContext(request, {'latest_question_list' :
latest_question_list, })
 return HttpResponse(template.render(context))

 def detail(request, question_id):
 try:
 question = Question.objects.get(pk = question_id)
 except Question.DoesNotExist:
 raise Http404("Question ne postoji")
 return render(request, 'polls/detail.html', {'question': question})

 def results(request, question_id):
 question = get_object_or_404(Question, pk = question_id)
 return render(request, 'polls/results.html', {'guestion':
question})


 def vote(request, question_id):
 p = get_object_or_404(Question, pk=question_id)
 try:
 selected_choice  = p.choice_set.get(pk=request.POST['choice'])
 except (KeyError, Choice.DoesNotExist):
 return render(request, 'polls/detail.html', {
 'question': p,
 'error_message': "Nijesi izabrao pitanje.",
  })
 else:
 selected_choice.votes += 1
 selected_choice.save()
 return HttpResponseRedirect(reverse('polls:results', args=(p.id
,)))
>>>
>>>
>>>
>>>
>>>
>>> --
>>> 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+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at http://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/2e2320f1-6813-4e01-8b32-76694dbd22f8%40googlegroups.com
.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> 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+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/CA%2Be%2BciUv65UQLmJKjWwySKga08gvAa0OHwkZ39hOj4CjPBS3ww%40mail.gmail.com
.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> "La utopía sirve para caminar" Fernando Birri
>
> <
https://ci3.googleusercontent.com/proxy/pleYFBjRTaqPzwl0QWV68XGbm9_iKE548G9HnZip6o7gk46Dni4svLKWIxqanv21wf4L-oDTIVyWUviHHYQOxYJ_VR-E2QHnDRBqrtZefM09Bgx09obS9PzSCP77HVIAehvKQVoH7RXKVRwJcXB8ZhPlAlEmO_2MsbjN1b

Re: NoReverseMatch at /polls/1/results/

2015-05-08 Thread Muhammad M
HM,

Upon a closer look at your code (views.results), you have this:

return render(request, 'polls/results.html', {'guestion': question})

You are passing "guestion" (with a G) to the template instead of "question"
(with a Q).

As such, your template complains when you try to use "question.id" because
it is expecting "guestion.id" (with a G).

Change it to {'question': question} (with a Q) in views.results and see how
it goes again.

All the best. :)

Sincerely,
Muhammad
On May 8, 2015 10:41 PM, "Luis Zárate"  wrote:

> Which urls.py you paste here? The project URLs or the app urls .?
>
> It is because you are using namespace in the url reverse so you need to
> named in your project's URLs and put the code paste here in your app urls.
>
> El jueves, 7 de mayo de 2015, James Schneider 
> escribió:
> > I'm guessing the issue is actually in your template if you are following
> along that page. None of the code in your views would generate that error
> from what I can see.
> >
> > The error indicates that you are trying to reverse('polls:detail') and
> not providing any arguments for the URL resolver (or are passing an
> empty/missing variable). In the template, this would probably look
> something like {% url 'polls:detail' %}. I would guess that you are
> forgetting question.id after the view name, assuming you are using the
> same variable names as the tutorial.
> >
> > Check near the bottom of the stack trace on the page, I bet it has a {%
> url %} tag highlighted as the culprit.
> >
> > -James
> >
> > On May 7, 2015 12:09 AM, "H M"  wrote:
> >>
> >> I am on part 4 django tutorial. The tutorial is very good and I easily
> get on part 4 but
> >>
> >> I get error: Reverse for 'detail' with arguments '('',)' and keyword
> arguments '{}' not found. 1 pattern(s) tried:
> [u'polls/(?P\\d+)/$']
> >>
> >> Am I missing something?
> >>
> >> My code is following:
> >>
> >>> urlpatterns = patterns('',
> >>> url(r'^$', views.index, name='index'),
> >>> url(r'^(?P\d+)/$', views.detail, name='detail'),
> >>> url(r'^(?P\d+)/results/$',views.results,
> name='results'),
> >>> url(r'^(?P\d+)/vote/$', views.vote, name='vote'),
> >>
> >>
> >> views.py:
> >>
> >>> from django.shortcuts import get_object_or_404, render
> >>> from django.http import HttpResponseRedirect, HttpResponse
> >>> from django.template import RequestContext, loader
> >>> from polls.models import Question, Choice
> >>> from django.http import Http404
> >>> from django.core.urlresolvers import reverse
> >>>
> >>>
> >>> def index(request):
> >>> latest_question_list =  Question.objects.order_by('-pub_date')[:5]
> >>> template = loader.get_template('polls/index.html')
> >>> context = RequestContext(request, {'latest_question_list' :
> latest_question_list, })
> >>> return HttpResponse(template.render(context))
> >>>
> >>> def detail(request, question_id):
> >>> try:
> >>> question = Question.objects.get(pk = question_id)
> >>> except Question.DoesNotExist:
> >>> raise Http404("Question ne postoji")
> >>> return render(request, 'polls/detail.html', {'question': question})
> >>>
> >>> def results(request, question_id):
> >>> question = get_object_or_404(Question, pk = question_id)
> >>> return render(request, 'polls/results.html', {'guestion':
> question})
> >>>
> >>>
> >>> def vote(request, question_id):
> >>> p = get_object_or_404(Question, pk=question_id)
> >>> try:
> >>> selected_choice  = p.choice_set.get(pk=request.POST['choice'])
> >>> except (KeyError, Choice.DoesNotExist):
> >>> return render(request, 'polls/detail.html', {
> >>> 'question': p,
> >>> 'error_message': "Nijesi izabrao pitanje.",
> >>>  })
> >>> else:
> >>> selected_choice.votes += 1
> >>> selected_choice.save()
> >>> return HttpResponseRedirect(reverse('polls:results', args=(
> p.id,)))
> >>
> >>
> >>
> >>
> >>
> >> --
> >> 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+unsubscr...@googlegroups.com.
> >> To post to this group, send email to django-users@googlegroups.com.
> >> Visit this group at http://groups.google.com/group/django-users.
> >> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/2e2320f1-6813-4e01-8b32-76694dbd22f8%40googlegroups.com
> .
> >> For more options, visit https://groups.google.com/d/optout.
> >
> > --
> > 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+unsubscr...@googlegroups.com.
> > To post to this group, send email to django-users@googlegroups.com.
> > Visit this group at http://groups.google.com/group/django-users.
> > To view this discussion on the web visit

Re: NoReverseMatch at /polls/1/results/

2015-05-08 Thread James Schneider
I agree that the typo is also an issue and should be fixed, but that
wouldn't result in the OP's error, since reverse() is complaining about a
'detail' URL specifically. The typo would result in a similar error when
the result page is displayed, and would show 'guestion' as one of the
kwargs.

-James
On May 8, 2015 8:10 PM, "Muhammad M"  wrote:

> HM,
>
> Upon a closer look at your code (views.results), you have this:
>
> return render(request, 'polls/results.html', {'guestion': question})
>
> You are passing "guestion" (with a G) to the template instead of
> "question" (with a Q).
>
> As such, your template complains when you try to use "question.id"
> because it is expecting "guestion.id" (with a G).
>
> Change it to {'question': question} (with a Q) in views.results and see
> how it goes again.
>
> All the best. :)
>
> Sincerely,
> Muhammad
> On May 8, 2015 10:41 PM, "Luis Zárate"  wrote:
>
>> Which urls.py you paste here? The project URLs or the app urls .?
>>
>> It is because you are using namespace in the url reverse so you need to
>> named in your project's URLs and put the code paste here in your app urls.
>>
>> El jueves, 7 de mayo de 2015, James Schneider 
>> escribió:
>> > I'm guessing the issue is actually in your template if you are
>> following along that page. None of the code in your views would generate
>> that error from what I can see.
>> >
>> > The error indicates that you are trying to reverse('polls:detail') and
>> not providing any arguments for the URL resolver (or are passing an
>> empty/missing variable). In the template, this would probably look
>> something like {% url 'polls:detail' %}. I would guess that you are
>> forgetting question.id after the view name, assuming you are using the
>> same variable names as the tutorial.
>> >
>> > Check near the bottom of the stack trace on the page, I bet it has a {%
>> url %} tag highlighted as the culprit.
>> >
>> > -James
>> >
>> > On May 7, 2015 12:09 AM, "H M"  wrote:
>> >>
>> >> I am on part 4 django tutorial. The tutorial is very good and I easily
>> get on part 4 but
>> >>
>> >> I get error: Reverse for 'detail' with arguments '('',)' and keyword
>> arguments '{}' not found. 1 pattern(s) tried:
>> [u'polls/(?P\\d+)/$']
>> >>
>> >> Am I missing something?
>> >>
>> >> My code is following:
>> >>
>> >>> urlpatterns = patterns('',
>> >>> url(r'^$', views.index, name='index'),
>> >>> url(r'^(?P\d+)/$', views.detail, name='detail'),
>> >>> url(r'^(?P\d+)/results/$',views.results,
>> name='results'),
>> >>> url(r'^(?P\d+)/vote/$', views.vote, name='vote'),
>> >>
>> >>
>> >> views.py:
>> >>
>> >>> from django.shortcuts import get_object_or_404, render
>> >>> from django.http import HttpResponseRedirect, HttpResponse
>> >>> from django.template import RequestContext, loader
>> >>> from polls.models import Question, Choice
>> >>> from django.http import Http404
>> >>> from django.core.urlresolvers import reverse
>> >>>
>> >>>
>> >>> def index(request):
>> >>> latest_question_list =  Question.objects.order_by('-pub_date')[:5]
>> >>> template = loader.get_template('polls/index.html')
>> >>> context = RequestContext(request, {'latest_question_list' :
>> latest_question_list, })
>> >>> return HttpResponse(template.render(context))
>> >>>
>> >>> def detail(request, question_id):
>> >>> try:
>> >>> question = Question.objects.get(pk = question_id)
>> >>> except Question.DoesNotExist:
>> >>> raise Http404("Question ne postoji")
>> >>> return render(request, 'polls/detail.html', {'question':
>> question})
>> >>>
>> >>> def results(request, question_id):
>> >>> question = get_object_or_404(Question, pk = question_id)
>> >>> return render(request, 'polls/results.html', {'guestion':
>> question})
>> >>>
>> >>>
>> >>> def vote(request, question_id):
>> >>> p = get_object_or_404(Question, pk=question_id)
>> >>> try:
>> >>> selected_choice  = p.choice_set.get(pk=request.POST['choice'])
>> >>> except (KeyError, Choice.DoesNotExist):
>> >>> return render(request, 'polls/detail.html', {
>> >>> 'question': p,
>> >>> 'error_message': "Nijesi izabrao pitanje.",
>> >>>  })
>> >>> else:
>> >>> selected_choice.votes += 1
>> >>> selected_choice.save()
>> >>> return HttpResponseRedirect(reverse('polls:results', args=(
>> p.id,)))
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> --
>> >> 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+unsubscr...@googlegroups.com.
>> >> To post to this group, send email to django-users@googlegroups.com.
>> >> Visit this group at http://groups.google.com/group/django-users.
>> >> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/2e2320f1-6813-4e01-8b32-76694dbd22f8%40googlegroups.com
>> .
>> >> Fo