I have accumulated a few small notes / tips about the python and django, which in some topics do not draw, so published all at once.
Django: simplify vyuh code
The documentation and training examples for django usually write something like this:
def contact (request):
if request.method == ‘POST’:
form = ContactForm (request.POST)
if form.is_valid ():
# Process the data. For example, do form.save ()
# …
return HttpResponseRedirect (‘/ thanks /’) # after a POST-request to redirect
else:
form = ContactForm ()
return render_to_response (‘contact.html’, (
‘Form’: form,
), Context_instance = RequestContext (request))
Perhaps this is correct – so explain to people better understand what was happening.
Chips:
* Always use django.shortcuts.redirect for redirects. It knows the names of reversion URLs cause get_absolute_url or simply redirect to a URL
* Use django.views.generic.simple.direct_to_template instead render_to_response. They are doing almost the same thing, but direct_to_template use RequestContext instead of Context, which is already required in most cases. Instead direct_to_template can use decorator render_to of django-annoying, but it is a matter of taste, as you like.
Django: Draw graphics
In the article, the admin has promised to tell you about graphics, but still could not reach his hands, badly turned out. Yes, and tell something there is nothing special, all too simple and “stupid” – schedules are drawn through the google charts. In this case, you can do without libraries: design currently schedule to taste here (it’s semi-official tool from Google, it has a link with information on the google api charts), and then insert the resulting string into the template and substitute the variables in place of the test values.
There is a very thin wrapper over the google charts: django-chart-tools. Essence – is the same: to collect chart visually and replace the variables, just to django-chart-tools such charts are easier to maintain.
Data sampling can be done simply by django ORM, or for the convenience / speed through the django-qsstats-magic, depending on the task.
As a result (using django-chart-tools and django-qsstats-magic) users schedule on days can derive something like this:
# Original data
qs = User.objects.filter (is_active = True)
end = datetime.today ()
start = end-timedelta (days = 30)
# Prepare the data for the chart
data = QuerySetStats (qs, ‘date_joined’). time_series (start, end)
values = [t [1] for t in data]
captions = [t [0]. day for t in data]
then the variable values and captions convey to the template, and then derive the schedule as follows:
(% Load chart_tags%)
(% Bar_chart values captions «580×100»%)
Restrictions on the number of appeals from google, charts image api is not there just ask to contact them if 200tys hits per day will be that they have distributed the load. So that such graphs can not only use the admin panel.
Django: Tests
Use django-webtest for writing tests. I have wrote about this application in past, but since that time there has not been very important change: django-webtest now provides access to the context of patterns (in the same way as the standard test client).
Now you can write in this style:
# …
response = page.forms ['my-form-id']. submit (). follow ()
assert response.context ['user'] == self.user
works well as a standard assertTemplateUsed.
django-webtest is better than any integration with twill, because they do not have access to context templates and full support for Unicode, and will not develop.
django-webtest better than the standard test client, as provides a simple API (just try zasabmitit form with default values through the standard test-client). With a standard test client also can not test the absence csrf-token (or a very roundabout puyami black), and django-webtest is done is trivial (and even automatically). Use django-webtest)
There would pop up a sign with features done: the django-webtest will be everywhere the green check mark and a twill and a standard test client – red here and there.
Useful suggestions appreciated.






A lot of surfers who use Proxy Servers frequently complain that they do not work at their school/office computers because their Web proxies are blocked by the system administrator.