django-views

How To Clear self.add_error in Django if user clicks on browser back button?

十年热恋 提交于 2019-12-11 18:05:35
问题 I have a FORMVIEW where a user selects a value from a dropdown, and if they select the value, and click submit, it performs an HTTPResponseRedirect to a URL and all is well. If they forget to enter a value and click submit, they get an error message telling them to choose a value. I do a clean on the form to figure this out. This all works just fine. The issue is if the user gets an error message, then adds a value and then displays the desired output. If they then click the back button on

How To Narrow Down Django DetailView Export To CSV File

落花浮王杯 提交于 2019-12-11 17:57:27
问题 I am trying to figure out how to do a simple export of data to a CSV file. I found an example that works.... def export_data(request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="users.csv"' writer = csv.writer(response) writer.writerow(['username', 'First name', 'Last name', 'Email address']) users = User.objects.all().values_list('name','userid') for user in users: writer.writerow(user) return response The code above works as you

In a Django Formset being returned in a Post, all Forms are showing all fields changed even though none have

懵懂的女人 提交于 2019-12-11 17:55:01
问题 This is a non-model based Form: from django import forms class MeasureForm(forms.Form): name = forms.CharField(max_length=15) description = forms.CharField(max_length=100) datatype = forms.CharField(max_length=20) uom = forms.Char Populated from a non-model based view: def define_measures(request): measureformset = formset_factory(MeasureForm, extra=2) if request.method == 'POST': # If the form has been submitted... formset = measureformset(request.POST ) # A form bound to the POST data if

Editing an object with HTML form instead of Django model form

僤鯓⒐⒋嵵緔 提交于 2019-12-11 17:52:43
问题 I am trying to edit my objects using HTML form instead of Django model form. I need to know how to make the view for this ( See my incomplete view below in bold ) Intro: I have a very simple html (invisible )form in the right column of my Index.html. when the user clicks on Locate me button. The form automatically fills the users latitude and longitude details and clicks submit(the user does not see the form). I use jQuery to achieve this <form method="post" action="#"> {% csrf_token %}

Insert for loop item in django output field

假如想象 提交于 2019-12-11 17:51:02
问题 I have a for loop in my django template: {% for item in data %} {{ item.Load}} {{ item.To }} {{ item.From }} {{ item.Weight }} and I want to parse item.load in this format {{ forminput.field.value|default_if_none:"" }} but when I do this, {{ forminput.field.item.Load|default_if_none:"" }} it doesn't show my field in the django form. When I enter a static value(e.g. 3) in this it appears in the form like this {{ forminput.Load_ID.3|default_if_none:"" }} it shows the 4th object on the page. How

Do I have to re-render my page after updating template data

Deadly 提交于 2019-12-11 17:41:50
问题 First I have searched and seen another answer, but it doesn't address my need. I am trying to POST data using jQuery/AJAX from my HTML to update a list that is also on my HTML. When I first rendered my template, it had two list-group containers of which the left container is pre populated. two containers The choice made user makes on the left container determines the list group data of the right container . I wanted to send back to the backend Python (server) which selection the user made

Not allowing images small than certain dimensions

南楼画角 提交于 2019-12-11 17:28:27
问题 I have a model that saves user profile images. If the image that is uploaded is greater than 200x200 pixels, then we resize to 200x200. If the image is right at 200x200, then we return that image. What I want now is to throw an error to the user saying that this image is too small and is not allowed. Here's what I have: class Profile(models.Model): GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) user = models.OneToOneField(User, null=True, on_delete=models.CASCADE) bio = models.CharField

Not getting the value of a form in Django

£可爱£侵袭症+ 提交于 2019-12-11 17:27:43
问题 In Django I want to open a URL (this is posted by a user in a form) and after clicking the submit button (of the form) with the URL, get the data (the URL) and do some things with it (printing it in the view). I only have one function in the views.py file, so I guess my main problem is that I should have two. But I am not really sure how to solve it, however have I tried. My form is this one: <html> <head> <title> Practica Django </title> </head> <form method="POST" action="" id = "loginForm"

TypeError at /signup 'NoneType' object is not callable in Django

倖福魔咒の 提交于 2019-12-11 17:20:05
问题 Have custom User model, but using this model I can only create superuser using terminal command "python manage.py createsuperuser". The error : "TypeError at /signup 'NoneType' object is not callable" Traceback: 1 userV= userM.create_user(int(Mno),Role,Pass) 2 mobile_no=mobile_no, models.py class Users(AbstractBaseUser, PermissionsMixin): object = UserManager() mobile_no = models.IntegerField(_('MobNumber'), null=True, blank=True,unique=True) role = models.CharField(_('Role'), max_length=70,

How to split one form fields into multiple fields of a model in django?

痞子三分冷 提交于 2019-12-11 16:59:16
问题 This question is similar to Using multiple input fields for one model's attribute with django and How to render two form fields as one field in Django forms? What I need is basically the opposite of MultiValueField where I can save data from one form field - Name into two model fields - Firstname, Lastname . I'm unable to find any such thing like the opposite of MultiValueField. Can someone please suggest me what is the better way to do it. 回答1: Why don't you define the name field in Form and