django-views

Django internationalization language codes [closed]

主宰稳场 提交于 2019-12-17 22:22:25
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Where can I find list of languages and language_code like this. (Swedish,sv) (English,en) 回答1: Wiki: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes 回答2: If you want something you can use from within django, try: from django.conf import settings this will be in the format above, making it perfect for

How to pass extra_context when redirecting in Django

为君一笑 提交于 2019-12-17 20:42:35
问题 My views.py: @login_required def some_views(request): if request.method == 'POST': form = AddressCreateFrom(request.POST) if form.is_valid(): name = form.cleaned_data['Address'] ip_value = form.cleaned_data['value'] user_list = get_username(name) address_create = form.save() extra_context = { 'user_list': user_list } return redirect_to(request, url=address_create.get_absolute_url()) else: form = AddressCreateFrom() extra_context = { 'form':AddressCreateFrom(initial={'user': request.user.pk})

render_to_response or redirect changes the template elements in Django 1.8

可紊 提交于 2019-12-17 20:39:17
问题 I'm trying to check if email id entered by user is existing in the database table, if existing - I would like to route to 'prof.html' template otherwise just show a message in the login.html template. Both the conditions are working fine. However, the problem is when I use redirect() or render_to_response() - the destination template elements like div, input etc., are being changed automatically (prof.html in this case) ? Can we also send the context information to destination template ?

Django UpdateView without pk in url

不问归期 提交于 2019-12-17 18:38:39
问题 Is it possible eliminate pk from url related to UpdateView ? For example, if I have url(r'^myobj/update/(?P<pk>\d+)/$', views.UpdateMyObj.as_view(), name="update") is there any way to write it like url(r'^myobj/update/$', views.UpdateMyObj.as_view(), name="update") and then send pk as a parameter in POST or GET request? 回答1: Yes it is possible you just need to override the get_object method: from django.views.generic.edit import UpdateView class UpdateMyObj(UpdateView): # ..... def get_object

Implementing Ajax requests / response with django-allauth

末鹿安然 提交于 2019-12-17 18:25:06
问题 I am using django-allauth for one of my project. I would like to implement login/signup process via ajax. I would like to have customized signup form. I was going through their signupmixin and signup form. Sounds like I can write custom views for each action and map it to the url config. I am not sure what is the best way to do this. Thank you so much for any help or advice on this. 回答1: It depends a bit on what you mean by ajax. If you just want to have a popup-style login/signup box on

Render a CheckBoxSelectMultiple form using the data present in Database. [initial value is a queryset from DB]

回眸只為那壹抹淺笑 提交于 2019-12-17 17:16:08
问题 I have a form called DemoForm which is related to model Demo class Demo(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) ans = models.CharField(max_length=1024) and the form for this is class DemoForm(forms.ModelForm): class Meta: model = Demo exclude = ('user',) widgets = {'ans': forms.CheckboxSelectMultiple} I want to render this form using a queryset I have tried different approaches like form = DemoForm(initial=Love.objects.filter(user=request.user)) <form=GoodForm(

Using multiple forms on a page in Django

ぃ、小莉子 提交于 2019-12-17 15:52:00
问题 How do I use more than one form per page in Django? 回答1: Please see the following previously asked (and answered) questions: Django: multiple models in one template using forms and Proper way to handle multiple forms on one page in Django. Depending on what you're really asking, this is my favorite way to handle different models on the same page: if request.POST(): a_valid = formA.is_valid() b_valid = formB.is_valid() c_valid = formC.is_valid() # we do this since 'and' short circuits and we

django @login_required decorator for a superuser

回眸只為那壹抹淺笑 提交于 2019-12-17 15:34:38
问题 Is there a decorator in django similar to @login_required that also tests if the user is a superuser? Thanks 回答1: Use the user_passes_test decorator: from django.contrib.auth.decorators import user_passes_test @user_passes_test(lambda u: u.is_superuser) def my_view(request): ... 回答2: In case staff membership is sufficient and you do not need to check whether the user is a superuser, you can use the @staff_member_required decorator: from django.contrib.admin.views.decorators import staff

How do I call a Django function on button click?

夙愿已清 提交于 2019-12-17 10:24:32
问题 I am trying to write a Django application and I am stuck at how I can call a view function when a button is clicked. In my template, I have a link button as below, when clicked it takes you to a different webpage: <a target="_blank" href="{{ column_3_item.link_for_item }}">Check It Out</a> When the button is clicked, I also want to call a Django view function (along with re-direct to a target website). The view function increments the value in the database which stores the number of times the

Override a form in Django admin

不羁的心 提交于 2019-12-17 08:49:30
问题 In Django admin I want to override and implement my own form for a model (e.g. Invoice model). I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a customer). How can I override the default form provided by Django admin and implement my own? I am new to Django, I appreciate any pointers. 回答1: You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form