Can I make STATICFILES_DIR same as STATIC_ROOT in Django 1.3?

No. In fact, the file django/contrib/staticfiles/finders.py even checks for this and raises an ImproperlyConfigured exception when you do so: “The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting” The STATICFILES_DIRS can contain other directories (not necessarily app directories) with static files and these static files will be collected into your STATIC_ROOT when you run collectstatic. … Read more

Difference between APIView class and viewsets class?

APIView is the most basic class that you usually override when defining your REST view. You usually define your methods like get, put, delete and others see: http://www.cdrf.co/3.5/rest_framework.views/APIView.html. With APIView you define your view and add it to your urls like so: # in views.py class MyAPIView(APIView): … #here you put your logic check methods … Read more

Can I have a Django form without Model

Yes. This is very much possible. You can read up on Form objects. It would be the same way you would treat a ModelForm, except that you are not bound by the model, and you have to explicitly declare all the form attributes. def form_handle(request): form = MyForm() if request.method==’POST’: form = MyForm(request.POST) if form.is_valid(): … Read more

Django serve static index.html with view at ‘/’ url

You can serve static/index.html for development like this: if settings.DEBUG: urlpatterns += url( r’^$’, ‘django.contrib.staticfiles.views.serve’, kwargs={ ‘path’: ‘index.html’, ‘document_root’: settings.STATIC_ROOT}), But for production you should configure your nginx (or other frontend server) to serve index.html file for / location UPDATE I want to explain the case you should do like this. For example your django … Read more

How do I invalidate @cached_property in django

Just del it as documentation says. It will lead to recalculation on next access. class SomeClass(object): @cached_property def expensive_property(self): return datetime.now() obj = SomeClass() print obj.expensive_property print obj.expensive_property # outputs the same value as before del obj.expensive_property print obj.expensive_property # outputs new value For Python 3 it’s the same use of del. Below is an … Read more

How do I call a Django function on button click?

here is a pure-javascript, minimalistic approach. I use JQuery but you can use any library (or even no libraries at all). <html> <head> <title>An example</title> <script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js”></script> <script> function call_counter(url, pk) { window.open(url); $.get(‘YOUR_VIEW_HERE/’+pk+”https://stackoverflow.com/”, function (data) { alert(“counter updated!”); }); } </script> </head> <body> <button onclick=”call_counter(‘http://www.google.com’, 12345);”> I update object 12345 </button> <button onclick=”call_counter(‘http://www.yahoo.com’, 999);”> … Read more