Django url field test issue

Can someone clarify to me why this url http://www.nacolmeia.com.br/do/Home/oferta/EnER not accepted in the form generated from Django's URLField?

:)

thanks

+6
source share
2 answers

Do you host the site from the same server on which you are trying to check it? docs

Please note that when you use a single-threaded development server, checking the URL served by the same server will hang. This should not be a problem for multithreaded servers.

This is not like its form level fault tolerance

 >>> from django import forms >>> f = forms.URLField() >>> f.clean('http://www.nacolmeia.com.br/do/Home/oferta/EnER') u'http://www.nacolmeia.com.br/do/Home/oferta/EnER' >>> f.clean('sadfas') Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/dev/.virtualenvs/thepidb/lib/python2.7/site-packages/django/forms/fields.py", line 171, in clean self.run_validators(value) File "/home/dev/.virtualenvs/thepidb/lib/python2.7/site-packages/django/forms/fields.py", line 160, in run_validators raise ValidationError(errors) ValidationError: [u'Enter a valid URL.'] >>> 

If you do not need to verify that the website does not return 404, in your models.py file

 url = models.URLField(verify_exists=False) 

change

after some digging in the django here source code, and some were messing around with the shell, I'm still not sure why the URL with the headers causes a redirect loop.

 >>> from django.core.validators import URLValidator >>> u = URLValidator(verify_exists=True) >>> u.__call__('http://www.nacolmeia.com.br/do/Home/oferta/EnER') Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/dev/.virtualenvs/thepidb/lib/python2.7/site-packages/django/core/validators.py", line 105, in __call__ raise broken_error ValidationError: [u'This URL appears to be a broken link.'] >>> u.__call__('http://www.nacolmeia.com.br/do/home/oferta/ener') >>> 

An exception to the exception is an HTTPError:

  File "/usr/lib/python2.7/urllib2.py", line 606, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python2.7/urllib2.py", line 398, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 511, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 430, in error result = self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 370, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 606, in http_error_302 return self.parent.open(new, timeout=req.timeout) File "/usr/lib/python2.7/urllib2.py", line 398, in open response = meth(req, response) File "/usr/lib/python2.7/urllib2.py", line 511, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/python2.7/urllib2.py", line 430, in error result = self._call_chain(*args) File "/usr/lib/python2.7/urllib2.py", line 370, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 596, in http_error_302 self.inf_msg + msg, headers, fp) HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: Found >>> 

here are some posts talking about HTTPError: here and here

it looks like it has something to do with cookies, but I cannot offer a good explanation, I will leave it to someone else.

A workaround that might work if you don't want to turn off validation but don't care about capitalizing your URLs is to override the clean_field method of your forms.

 def clean_your_url_field(self): return self.cleaned_data['your_url_field'].lower() 
+7
source

I think I found the problem. When you open this url:

http://www.nacolmeia.com.br/do/home/oferta/ener

... it redirects this URL:

http://www.nacolmeia.com.br/do/Home/oferta/EnER/piracicaba/a-pascoa-chegou-na-planet-chokolate!-50-off-para-1-caixa-com-16-bombons -recheados - 1-pao-de-mel-recheado-ou-1-caixa-com-16-trufas-recheadas - 1-pao-de-mel-recheado-de-rs-47.10-por-rs-23.55 .

The first URL is ok, but the redirected is 247 characters. This "should not" be a problem, except that models.fields.URLField has max_length , which defaults to 200 characters. Therefore, it does not check because it is too long.

Instead, increase max_length and it should work: models.URLField(max_length=255) For information on the longest URL, see this SO question . It is definitely longer than 200 characters.

EDIT: It only redirects the second URL when setting the cookie! If you re-visit the same page again with an existing cookie, it simply displays a shorter URL.


But what about lower case? It seems your web server is case sensitive with respect to URLs, and in lower case:

http://www.nacolmeia.com.br/do/home/oferta/ener

... displays a common error page. It does not redirect the URL to 247 characters. So it passes the test, because the only thing that models .URLField takes care of; Does the webpage load or not?

0
source

Source: https://habr.com/ru/post/885466/


All Articles