django-views

django request.POST contains <could not parse>

社会主义新天地 提交于 2019-12-05 10:54:05
I am having a django form to get the username,password. when the user posts data, i see that the post dictionary contains the following(traceback), Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py", line 111, in get_response response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.4/site-packages/django/views/decorators/csrf.py", line 39, in wrapped_view resp = view_func(*args, **kwargs) File "/usr/lib/python2.4/site-packages/django/views/decorators/csrf.py", line 52, in wrapped_view return view_func(*args, *

What's the difference between the two methods of decorating class-based views?

混江龙づ霸主 提交于 2019-12-05 10:32:21
I'm writing a view that inherits from ListView, and am trying to restrict the view to logged-in users. https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-in-urlconf says that decorating with login_required in the URLconf "applies the decorator on a per-instance basis. If you want every instance of a view to be decorated, you need to take a different approach" -that approach being to decorate the dispatch method in the view code. I thought I knew the difference between a class and an instance but this phrase doesn't mean anything to me. Could someone clarify? Apart from

django pdf export

大城市里の小女人 提交于 2019-12-05 10:23:53
I want to generate a PDF which will show the output of my queryset in table format, for example: query = ModelA.objects.filter(p_id=100) class ModelA(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=200) p_id = models.IntegerField() description = models.TextField() I need to show the values for name , description and pid in the generated PDF. As mentioned by other people the best way to do this is to generate a template then convert the result, using one of the many libraries around, into a PDF. This method provides you with the usual amount of control

How to validate mulitple forms in a single formView class Django

北慕城南 提交于 2019-12-05 10:17:13
问题 I have a formView class as you can see below:- view.py class ThreadForm(FormView): template_name = 'thread.html' form_class = ThreadModelForm success_url = '/success' def form_valid(self, form): # This method is called when valid form data has been POSTed. # It should return an HttpResponse. print form.cleaned_data return super(ThreadForm, self).form_valid(form) def get_context_data(self, **kwargs): context = super(ThreadForm, self).get_context_data(**kwargs) context['second_form'] =

Django: from django.urls import reverse; ImportError: No module named urls [duplicate]

一笑奈何 提交于 2019-12-05 08:41:31
问题 This question already has answers here : “ImportError: No module named urls” while following Django Tutorial (3 answers) Closed 3 years ago . I am working through the DjangoProject tutorial with the polls app. As the tutorial states in part 4 I am trying to import 'reverse': from django.urls import reverse but getting the error: from django.urls import reverse ImportError: No module named urls I have changed the ROOT_URLCONF to just ' urls ', however, it did not work either. Any help is

Calling Django View from Ajax

穿精又带淫゛_ 提交于 2019-12-05 08:37:43
I'm using Ajax (along with Django) to perform some action on button click. I successfully call the javascript function but I can't call the Django view. There are no errors but the print statement in my view doesn't print...? urls.py urlpatterns = patterns('polls.views', url(r'^request_access/$', 'request_access', name='request_access'), ) views.py def request_access(request): print("DJANGO VIEW") if request.method == "POST": print("DATA: ", request.POST.get('request_data')) return HttpResponse( json.dumps(response_data), content_type="application/json" ) template.html <button class="btn btn

django form got multiple values for keyword argument

余生长醉 提交于 2019-12-05 08:35:23
问题 I have a simple model as follows: RATING_CHOICES = zip(range(1, 6), range(1, 6)) class Rating(models.Model): value = models.IntegerField(choices=RATING_CHOICES) additional_note = models.TextField(null=True, blank=True) from_user = models.ForeignKey(User, related_name='from_user') to_user = models.ForeignKey(User, related_name='to_user') shared_object = models.ForeignKey(ObjectDetail, null=True, blank=True) dtobject = models.DateTimeField(auto_now_add=True) From the above model I generate a

Django : Call a method only once when the django starts up

╄→гoц情女王★ 提交于 2019-12-05 07:54:03
I want to initialize some variables (from the database) when Django starts. I am able to get the data from the database but the problem is how should I call the initialize method . And this should be only called once. Tried looking in other Pages, but couldn't find an answer to it. The code currently looks something like this :: def get_latest_dbx(request, ....): #get the data from database def get_latest_x(request): get_latest_dbx(request,x,...) def startup(request): get_latest_x(request) Hui Zheng Some people suggest( Execute code when Django starts ONCE only? ) call that initialization in

Django HttpResponseRedirect vs render_to_response - how to get a login form to behave the way I need it to

风格不统一 提交于 2019-12-05 07:48:18
问题 I've already checked out the following stackoverflow question regarding the difference between HttpResponse, HttpResponseRedirect, and render_to_response, as well as having gone through the official django docs, but I'm really uncertain how best to get the functionality I'm looking to create. Right now I have an index.html with a login function (as seen in the views.py below) where the render_to_response that brings me to portal/index.html . However, as urls.py (see below) dictates, the url

AttributeError: '…' object has no attribute '***_set'

放肆的年华 提交于 2019-12-05 06:47:10
I have class Question(models.Model): created_by = models.ForeignKey(User) question = models.CharField(max_length=150) def __unicode__(self): return self.question class Answer(models.Model): rel = models.ForeignKey(Question) answer = models.CharField(max_length=150) def __unicode__(self): return self.answer and if i do answer1 = "xyz" tmp = Question.objects.get(pk=1) tmp.answer_set.create(answer=answer1) i get AttributeError: 'Question' object has no attribute 'answer_set' What is wrong? EDIT: Or what could i do next to solve this...or are there any alternatives? UPDATE: main/models.py: from