timi 1 Follower

I work by Tesobe for a practical year

Location: Berlin

Quick Links

View Show Available Profiles


Posted
timiDjango: Setting Headers in your Response (feed)

Today I found a interesting part in the Django Docs
about set and remove of Headers in responses .

Exanple
1
2
3
response = HttpResponse() # Created a HttpResponse
response['Cache-Control'] = 'no-cache'# Set Cache-Control Header
del response['Cache-Control'] # Remove Cache-Control Header

I know that should be do the web server like Apache but sometimes is it nicer (even recommended) to set in some
cases the headers in web app directly, to test the behaviour or to make really sure to get
the right headers in the response if you don’t have anytime access to the
server config’s or a lazy sys-admin.

For you interest:

Set and Remove in the Django Docs
Content-Disposition example


(This is an imported feed item. You can read the original item at <span class="http-markup"><a href="http://azd325.github.io/blog/2012/03/21/setting-headers-in-your-response/" rel="nofollow">http://azd325.github.io/blog/2012/03/21/setting-headers-in-your-response/</a></span>)

timi
1 month, 3 weeks ago, 0 Comments

timiDjango: Pre-Validation with the URL dispatcher (feed)

I work currently on a rating feature, to rate some items and I want to do this
in one view to handle the methods POST and GET.

POST should handle the submit request(rate count) of the item

GET should handle the template with the right item to see the current
user/ global rate

Situation:

  • max. stars = five
  • min. stars = one
  • zero stars are not possible to vote

What we need to rate?

  • item id
  • vote result

I start to add the first version of the view and I was adding two URL’s one for GET and one for POST.
And I what to handle the parameter over the named groups in Django.

.. code-block:: django

urlpatterns += patterns('',
    url(r'^ajax/votes/(?P\w+)/$', 'vote_for_item'),  # GET
    url(r'^ajax/votes/(?P\w+)/(?P\d+)/$', 'vote_for_item')  # POST
)

What we can to be more explicit?

We can use re of Python and so we can set a range of allowed numbers with [1-5].
So we have only a range of URL’s for every state of the rating situation and so we get a 404 if we want to access for example the vote-URL with the vote count of six.

.. code-block:: python

urlpatterns += patterns('',
    url(r'^ajax/votes/(?P\w+)/$', 'vote_for_item'),  # GET
    url(r'^ajax/votes/(?P\w+)/(?P[1-5])/$', 'vote_for_item')  # POST
)

The next thing we have to two exact same URL’s the only difference is that the POST needs an extra parameter.

Can we make this parameter optional?

Yeah, we can do it again with re. We have to wrap the vote part in (?:...) and after it a single ?. More in the Python Docs.

The end solution:

I have one URL for the cases of GET and POST and we create only the URL’s what I need for the rating feature

.. code-block:: python

urlpatterns += patterns('',
    url(r'^ajax/votes/(?P\w+)/(:?(?P[1-5])/)?$', 'vote_for_item'),
)

FYI:

Optional named groups Python re

URL dispatcher at Django Docs

re at Python Docs


(This is an imported feed item. You can read the original item at <span class="http-markup"><a href="http://azd325.github.io/blog/2012/03/22/pre-validation-with-the-url-dispatcher/" rel="nofollow">http://azd325.github.io/blog/2012/03/22/pre-validation-with-the-url-dispatcher/</a></span>)

timi
1 month, 3 weeks ago, 0 Comments

timiPycon 2012: Stop Writing Classes (feed)

Classes are great but they are also overused. This talk will describe examples of class overuse taken from real world code and refactor the unnecessary classes, exceptions, and modules out of them.


(This is an imported feed item. You can read the original item at <span class="http-markup"><a href="http://azd325.github.io/blog/2012/03/23/pycon-2012-stop-writing-classes/" rel="nofollow">http://azd325.github.io/blog/2012/03/23/pycon-2012-stop-writing-classes/</a></span>)

timi
1 month, 3 weeks ago, 0 Comments

timiEmpthy folder in Git (feed)

How do I add an empty directory to a git repository

The solution is:

.gitignore
1
2
3
4
# Ignore everything in this directory
*
# Except this file
!.gitignore

For your interest:

Stackoverflow Question


(This is an imported feed item. You can read the original item at <span class="http-markup"><a href="http://azd325.github.io/blog/2012/05/31/empty-folder-in-git/" rel="nofollow">http://azd325.github.io/blog/2012/05/31/empty-folder-in-git/</a></span>)

timi
1 month, 3 weeks ago, 0 Comments

timiDjango: Optional ForeignKey (feed)

Do make an optional Foreignkey in Django you have set “blank=True” and “null=True”.


(This is an imported feed item. You can read the original item at <span class="http-markup"><a href="http://azd325.github.io/blog/2012/06/08/django-optional-foreignkey/" rel="nofollow">http://azd325.github.io/blog/2012/06/08/django-optional-foreignkey/</a></span>)

timi
1 month, 3 weeks ago, 0 Comments

Followers (1)

Alex
Inbox
This Profile appears not to have received any Evis via its Follows.

Follows (0)

To add Follows, click the "Follow" link on any Profile you think is relevant.