Is this the way to validate Django model fields?

Firstly, you shouldn’t override full_clean as you have done. From the django docs on full_clean: Model.full_clean(exclude=None) This method calls Model.clean_fields(), Model.clean(), and Model.validate_unique(), in that order and raises a ValidationError that has a message_dict attribute containing errors from all three stages. So the full_clean method already calls clean, but by overriding it, you’ve prevented it … Read more

Django ModelForm override widget

If you want to override the widget for a formfield in general, the best way is to set the widgets attribute of the ModelForm Meta class: To specify a custom widget for a field, use the widgets attribute of the inner Meta class. This should be a dictionary mapping field names to widget classes or … Read more

Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip

You must set a container’s main process to bind to the special 0.0.0.0 “all interfaces” address, or it will be unreachable from outside the container. In Docker 127.0.0.1 almost always means “this container”, not “this machine”. If you make an outbound connection to 127.0.0.1 from a container it will return to the same container; if … Read more

Django – CSRF verification failed

The fix 1. include {% csrf_token %} inside the form tag in the template. 2. if for any reason you are using render_to_response on Django 1.3 and above replace it with the render function. Replace this: # Don’t use this on Django 1.3 and above return render_to_response(‘contact.html’, {‘form’: form}) With this: return render(request, ‘contact.html’, {form: … Read more

Django templates – split string to array

Django intentionally leaves out many types of templatetags to discourage you from doing too much processing in the template. (Unfortunately, people usually just add these types of templatetags themselves.) This is a perfect example of something that should be in your model not your template. class Game(models.Model): … def screenshots_as_list(self): return self.screenshots.split(‘\n’) Then, in your … Read more

Django cannot find static files. Need a second pair of eyes, I’m going crazy

do the following: If you are in DEBUG, set STATICFILES_DIRS = (“path/to/static”) variable in your settings.py. It should then work only in DEBUG mode. If you want it to also work in deploy mode, set STATIC_ROOT = (“path/to/static_root”) variable in the settings.py. Then, execute python manage.py collectstatic and it should also work. And now, for … Read more