django-forms

Django - empty form cannot be saved in database

浪尽此生 提交于 2020-01-25 07:49:05
问题 I have a form in which, user specifies lawyer-spec and save the data to the database. However I get an error that **null value in column "lawyer_spec" violates not-null constraint** So the data from the form is not processed properly. EDIT: mthod form_invalid prints empty line, and then two numbers ('pk' and 'lawyer_id') forms.py class MakeAppointmentForm(forms.ModelForm): first_name = forms.CharField(required=True) class Meta: model = CalendarAppointmentsReserved fields = ['case'] def __init

How to update a ImageField from views in django

谁说我不能喝 提交于 2020-01-25 07:08:13
问题 Im a bit frustared trying to update a photo from a form. I have this: <form action="/subir-fotos/{{campana_id}}/{{point_id}}/" method="POST" enctype="multipart/form-data" id="form_tomaFoto{{page}}">{% csrf_token %} <input type="hidden" value="{{i.titulo_foto.id}}" name="titulo"> <p><input type="file" accept="image/*" name="foto" required></p> <p><input type="submit" value="Enviar foto" class="boton"></p> </div> </form> My model: class InteraccionFotos(models.Model): campana = models

How to include a form in multiple templates

跟風遠走 提交于 2020-01-25 00:39:40
问题 I have a Django app with two types of users, lets call them A and B. The two user types have different navigation bars. User type A has a link in their navigation bar to allow them to send a message. The send message form comes up in an overlay, so the form needs to be present in every template, or fetched over AJAX. These users can send a message from any/every page on the site, and each page has its own view. If a message has errors, users should be returned to the same page they were on,

DISTINCT ON fields is not supported by this database backend

无人久伴 提交于 2020-01-24 23:32:47
问题 I am using distinct to get the distinct latest values but it is giving me an error: DISTINCT ON fields is not supported by this database backend views.py class ReportView(LoginRequiredMixin, generic.TemplateView): template_name = 'admin/clock/report.html' def get_context_data(self, **kwargs): context = super(ReportView, self).get_context_data(**kwargs) context['reports'] = TimesheetEntry.objects.filter( timesheet_jobs__job_company = self.request.user.userprofile.user_company, ).distinct(

Display image from ImageField by means of form

十年热恋 提交于 2020-01-23 09:58:17
问题 prelude : Here's the simpliest way to display an ImageField. Lets assume I have 10 fields in my form and I don't wanna iterate over all of them in template.html just to check if it's an imageField and display it in a different way. I want to handle it via form or widget or something like this. So I want the leave template.html as it's bellow. template.html <table> {{ form.as_table }} </table> And add to models.py an image_tag method: class UserProfile(Model): photo = ImageField(upload_to

How can I allow empty fields in my ModelForm while still using unique = True?

此生再无相见时 提交于 2020-01-23 01:04:17
问题 Currently in models.py I have class ModelName(models.Model): rowname = models.CharField(max_length=100, blank = True, unique=True) This does wonders as far as making sure the same value isn't submitted to the database twice but is there a way that I can have unique not raise an error when the value that is a duplicate is an empty string? Does unique take an exception argument? 回答1: Essentially, you need to follow the advice in this answer. While Django considers '' equal to '' for purposes of

How to render the radio buttons without ul in django

自古美人都是妖i 提交于 2020-01-22 19:52:27
问题 forms.py Date_Format = ( ('0', ' dd / mm / yyyy'), ('1', 'mm / dd / yyyy'), ) Time_Format = ( ('0', ' 12 hour AM / PM '), ('1', ' 24 hour '), ) class SettingsForm(forms.ModelForm): date_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Date_Format) time_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Time_Format) template.py Update: {% for radio in SettingsForm.date_format %} {{ radio.choice_label }} <div class="select">{{ radio.tag }}</div> {% endfor %} The above

Using Django Admin Actions to send bulk emails

こ雲淡風輕ζ 提交于 2020-01-22 15:32:32
问题 I'm looking for a way to send bulk emails to users from a Django Admin Action. This is what I have thus far: class MyUserAdmin(UserAdmin): list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', staff] list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active'] actions = ['send_EMAIL'] def send_EMAIL(self, request, queryset): from django.core.mail import send_mail for i in queryset: if i.email: send_mail('Subject here', 'Here is the message.', 'from@example.com',[i

django forms wizard and recaptcha

懵懂的女人 提交于 2020-01-22 14:31:25
问题 I did a recaptcha integration using the following django snippet settings.py RECAPTCHA_PUBLIC_KEY = '<your public key>' RECAPTCHA_PRIVATE_KEY = '<your private key>' #widgets.py from django import forms from django.utils.safestring import mark_safe from django.conf import settings from recaptcha import captcha class ReCaptcha(forms.widgets.Widget): recaptcha_challenge_name = 'recaptcha_challenge_field' recaptcha_response_name = 'recaptcha_response_field' def render(self, name, value, attrs

django form doesn't work on click submit

∥☆過路亽.° 提交于 2020-01-22 03:19:51
问题 I'm trying to do a form, with gender choices. The user could choice between male or female. What I have now in forms.py: class GenderForm(forms.Form): demo = DemoData.objects.all() GENDER_CHOICES = [ ('Male', 'Masculino'), ('Female', 'Feminino')] gender = forms.ModelChoiceField(demo, widget=Select(), required=True) choices_distlabel = [('', '')] + GENDER_CHOICES gender.choices = choices_distlabel in the template: <form action="" method="post"> {% for field in form_gender %} {{ field }} {%