Add data to ModelForm object before saving
form = CreateASomething(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.field1 = request.user obj.save()
form = CreateASomething(request.POST) if form.is_valid(): obj = form.save(commit=False) obj.field1 = request.user obj.save()
>>> students = Students.objects.all() # The queryset’s model class: >>> students.model project.app.models.Student # Name of the model class: >>> students.model.__name__ ‘Student’ # Import path of the models module: >>> students.model.__module__ ‘project.app.models’ # Django app name: >>> students.model._meta.app_label ‘app’
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
I can’t test it, but I bet you need return context at the end of get_context_data 🙂
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
I think you’re looking for {% include ‘_partial.html’ %}.
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
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
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
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