Is django-lazysignup allow_lazy_user decorator calling the wrapped view twice? - django

I'm using "django-lazysignup 0.8" with Django 1.3.
When I do this:
The view
#allow_lazy_user
def page_edit(request):
if request.method == 'GET':
if is_lazy_user(request.user):
b2 = Page.objects.create(user=request.user)
print request.user.username
return render_to_response('page_editor.html',{'page':b2})
the console output shows that the view seems to be called twice (b2 called twice and creates the Page object twice, and the print statment prints twice)
Here is the output screen :
Output:
7707089a583a424caf0face130cb20 # this is the reult of print request.user.username
[12/Mar/2012 15:02:45] "GET /edit/ HTTP/1.1" 200 8368
7707089a583a424caf0face130cb20
[12/Mar/2012 15:02:46] "GET /edit/images/favicon.ico HTTP/1.1" 200 8368
I don't need this to happen, the view should be called once and create one Page object. Is there any solution?

I don't think this is related to Django-lazysignup.
If you look at the url for the second request:
/edit/images/favicon.ico
That looks to me like your browser trying to load the favicon for your website. That suggests you've used a relative path images/favicon.ico instead of an absolute path /images/favicon.ico.
It doesn't seem quite right that the favicon url has called the page_edit view. This suggests your url pattern is missing a $ to denote the end of string. You should change it to something like:
url('^edit/$', 'page_edit'),

Related

How to print out request.body data in django?

Just working through the Django tutorials and playing around with stuff. Was going through the HttpResponse and HttpRequest docs and I'm just trying to print out data to see how things work.
However, when I try to print to console the request.body I get nothing back.
def detail(request, question_id):
current_question_selected = Question.objects.get(pk=question_id)
choices_for_question = current_question_selected.choice_set.all()
context = {"choices_for_question":choices_for_question, "current_question_selected":current_question_selected}
#print(request.META["REMOTE_USER"])
print(request.body)
return render(request, 'polls/detailtext.html', context)
This is what gets pritned to the screen, the letter 'b' with an empty string
[28/Jun/2022 10:59:56] "GET /polls/1/ HTTP/1.1" 200 456
b''
Not sure what i'm missing
The print displays an empty string because GET does not accept a body. Taken directly from the Mozilla Web APIs docs:
Note that a request using the GET or HEAD method cannot have a body and null is return in these cases.
If you want to pass data in a GET request, you need to pass them as parameters instead. Then, you can access the parameters using request.GET (HttpRequest.GET) or access them individually with request.GET.get('key').

Did not receive # and % symbols via Django GET URL

I was sending the get request like this:
http:121.0.0.1:8000/userlogin/userName=test&password=12345#%
but accepted like this:
"GET /userlogin?username=admin&password=12345 HTTP/1.1" 403 2868
The # and % symbols were not received.
I cannot understand what happened here?
URL parameters have certain allowed characters in them, as explained in this question: https://stackoverflow.com/a/1455639/1726625
I suggest editing your password settings, and allowing/disallowing those characters and/or changing the method that you're using to not passing them as a URL parameter.
Could you ellaborate on the case that you need that for?

View runs twice when HttpResponse if of 'application/pdf' type

I use Django 1.6.5.
I managed to view pdf in my google chrome using this code:
def generate_pdf(request):
...
with open("students.pdf", 'r') as pdf:
response = HttpResponse(pdf.read(), 'application/pdf')
return response
However I noticed that the view is called twice which isn't cool since I have pdf generation code in this view.
[31/May/2014 00:35:07] "GET /students/pdf/ HTTP/1.1" 200 18040
[31/May/2014 00:35:08] "GET /students/pdf/ HTTP/1.1" 200 18040
When I change the HttpResponse to look like this:
response = HttpResponse(pdf.read())
Then It will like the pdf opened in notepad. The pdf obviously doesn't render but the view is run only once.
Why adding just 'application/pdf' to my HttpResponse causes the view to run twice? :(
Before, I tested this only on Google Chrome. When testing on Firefox everything worked fine. What was more weird for me was that on my friend's laptop everything worked fine on chrome (by worked fine I mean view run only once). Then I had this thought - maybe some of the chrome extensions caused this? The anwer is YES.
My Google Chrome's AdBlocks caused the django view to run twice.

Can I pass non-URL definition view keyword to a view function when redirecting?

NoReverseMatch at /natrium/script/4c55be7f74312bfd435e4f672e83f44374a046a6aa08729aad6b0b1ab84a8274/
Reverse for 'run_details' with arguments '()' and keyword arguments '{'script_text': u'print "happy"', 'run_id': '6b2f9127071968c099673254fb3efbaf'}' not found.
This is an excerpt of my views.py
run_id = new_run.run_id
if not run_id:
raise AssertionError("bad run id")
# I tried with args=[run_id, clean['script_text']] too
return HttpResponseRedirect(reverse('run_details', kwargs={'run_id':run_id, 'script_text':clean['script_text']}))
which in turns calling this view function
def run_details(request, run_id, script_text):
"""
Displays the details of a given run.
"""
run = Run(run_id)
run.update(request.user)
codebundle = CodeBundle(run.cbid)
codebundle.update(request.user)
return render_response(request, "graphyte/runs/run_script.html",
{'run':run, 'codebundle':codebundle, 'files':run.artifacts, 'bundle':codebundle,
'source_code': script_text
})
Now this is my urls.py. The actual redirect views is in another app (kinda insane, but whatever...).
urlpatterns = patterns("webclient.apps.codebundles.views",
# many.....
url(r"^cb/newfolder/$", 'codebundle_newfolder', name="codebundle_newfolder"),
)
urlpatterns += patterns('webclient.apps.runs.views',
url(r"^run_details/(?P<run_id>\w+)/$", 'run_details', name="run_details"),)
This is getting really nasty for the last three hours. I am not sure what's going on. Can someone help me debug this?
Thanks.
The original plan did not have script_text, and I used args=['run_id'] only. It works. In other words, remove script_text from the two views everything will work.
EDIT
Sorry for the confusion. Script text is just a context variable that I need to pass to the reverse destination, and from there I render my template. The URLs should only display the run_id.
No, you can't really pass an 'extra keyword' to the view function when redirecting. I'll try to explain why.
When you return HttpResponseRedirect, Django returns a response with a 302 status code, and the new location.
HTTP/1.1 302 Found
Location: http://www.example.com/new-url/
Your browser will then usually fetch the new url, but that's a separate request. If your view needs a keyword, it needs to be included in that response somehow, unless you store state in the session. Your two options are
Include the extra keyword in the url:
http://www.example.com/new-url/keyword-value/
Include the extra keyword as a GET parameter
http://www.example.com/new-url/?keyword=keyword-value.
Then in your view, grab the keyword with keyword=request.GET['keyword']. Note that the keyword is no longer a kwarg in the view signature.
A third approach is to stick the keyword into the session before you redirect, then grab it out the session in the redirected view. I would advise against doing this because it's stateful and can cause odd results when users refresh pages etc.
Your run_details url doesn't accept a kwarg named script_text at all -- remove that from your reverse kwargs.

Django: View-function fails on saving model

I'm currently playing around with some selfmade online Text-RPG and now have to deal with some quite strange problem which I hope someone here can solve.
I'd like to toggle a boolean field called 'rp_willing' in the model 'Character' when clicking on certain links. For that I have a view function called 'character_toggle_rp_willing'.
The field definition:
class Character(models.Model):
...
rp_willing = models.BooleanField(default=False)
...
The view function:
#decorators.login_required
def character_toggle_rp_willing(request, id):
character = get_object_or_404(Character, pk=id)
character.rp_willing = not character.rp_willing
character.save()
return redirect(request.META.get('HTTP_REFERER','/accounts/overview'))
It queries the character in question from the database by it's id. Then 'rp_willing' is toggled and the character is saved before the function redirects back to the previous page.
The problem now seems to lie in the call of 'character.save()'. I've placed some print-functions for debugging purposes and everything else seems to work as intended. Also, the whole functions works absolutly correct if called from a template of the same app. But if the template is from a differente app, the save-function just isn't executed. There is no exception which could give any hints. I don't really know, how to debug in this case. I couldn't find the code of the built-in save()-function either.
Is the save()-function somehow related to the passed request? Or is there something I have just overlooked?
EDIT:
What the Dev-Server says...
Works as intended:
[28/Aug/2011 17:06:53] "GET /accounts/overview HTTP/1.1" 200 2283
[28/Aug/2011 17:06:59] "GET /accounts/character/toggle_rp_willing/1/ HTTP/1.1" 302 0
[28/Aug/2011 17:06:59] "GET /accounts/overview HTTP/1.1" 200 2285
Fails on saving:
[28/Aug/2011 17:03:26] "GET /village/ HTTP/1.1" 200 1531
[28/Aug/2011 17:03:28] "GET /accounts/character/toggle_rp_willing/1/ HTTP/1.1" 302 0
[28/Aug/2011 17:03:28] "GET /village/ HTTP/1.1" 200 1531
Okay, turned out I was just looking from a wrong angle. The problem is not my 'character_toggle_rp_willing' for it saves the character as it should. But the view it redirects to does not retrieve the character-data from the database. Instead it uses the data stored in a session variable - the old data. And then there is a method called, that saves that old data, so nothing changes in the end... I guess I should look for a good debugger.