Django: View-function fails on saving model - django

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.

Related

Django url not cannot find some UTF-8 usernames

i am experimenting with UTF-8 for usernames in a django app.
Django is Version 3.2.11
Python used is 3.9.4
Some users might have a profile visible to others and ther username in the url:
re_path("^u/(?P<username>\w+)/$", views.author_profile_view, name="author_profile_view"),
Normal Example works fine:
Browser shows -> /u/brainyChowder3/
Django shows -> GET /u/brainyChowder3/ HTTP/1.1" 200 10593
UTF-8 example 1 works also fine:
Browser shows -> /u/ɊȁⱲÒđΈⱦİĬd/
Django shows -> GET /u/%C9%8A%C8%81%E2%B1%B2%C3%92%C4%91%CE%88%E2%B1%A6%C4%B0%C4%ACd/ HTTP/1.1" 200 12508
But this UTF-8 does not work:
Browser shows -> /u/ɂáⱳ1⁄4%7Cĭğę
Django shows -> "GET /u/%C9%82%C3%A1%E2%B1%B31%E2%81%844%7C%C4%AD%C4%9F%C4%99 HTTP/1.1" 404 5585
The browser does show it strange, as he does not "translate" %7C to |, but that should be just optical?
Error shown is just
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/u/%C9%82%C3%A1%E2%B1%B31%E2%81%844%7C%C4%AD%C4%9F%C4%99
The current path, u/ɂáⱳ1⁄4|ĭğę, didn’t match any of these.
In Django shell I can query this user:
>>> User.objects.get(username='ɂáⱳ1⁄4|ĭğę')
<User: ɂáⱳ1⁄4|ĭğę>
The URI decoding looks ok to me.
I hope someone can explain why this is happening to one UTF-8 string, but not the other. Or maybe even knows how to fix it? :-D
I know it may not be the smartes thing to allow all UTF-8 for usernames, but this is more an experiment for me.
Thanks
The reason this happens has nothing to do with UTF-8, but with the fact that the username contains a non-word character (a character not matched by \w): a character that is not allowed for the <str:…> path converter. You can work with a <path:…>:
path('u/<path:username>/', some_view, name='some_name')

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?

Django - How to get the length of the array through GET request?

I have a http get request that has an array, particularly:
[29/Sep/2014 16:04:47] "GET /webapp/ajax_retreiveServerSideUsernames/?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5
Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&
columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=asc&start=0&length=10&searc
h%5Bvalue%5D=&search%5Bregex%5D=false&csrfmiddlewaretoken=Something&_=1412028280135 HTTP/1.1" 200
1436
My question is that how do I get the "length" of a particular array in a GET request?
For example the array order:order[i][column].
However, there may be cases with unkown array size.
If I know that i = 0, then I can use request.get['order[0][column]'] to get the content of the first array's column data.
I tried to use request.GET.getlist('order[column]') or request.GET.getlist('order') with no avail.

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.

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

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'),