django-forms

Change DateTime input format in django form

梦想与她 提交于 2020-01-21 11:07:06
问题 I am trying to change the DateTime format in one of my django forms. The format I would like it to accept is: day month year hour_minute_seconds timezone 11 Aug 2015 12:00:00 GMT -> %d %b %Y %H:%M:%S %Z I have been doing some tests but I have not been able to get the right way. I have disabled the L10N in the settings.py file so I can use the %b month format. This is my forms.py file content from django import forms from django.forms import DateTimeField from web.apps.customer.models import

Passing a user, request to forms

六月ゝ 毕业季﹏ 提交于 2020-01-19 07:34:09
问题 How would I pass a user object or a request to my form for validation? For example, I want to be able to do something like this -- class Form(forms.Form): ... def clean(self) user = request.user # how to get request.user here? user = User # how to pass the actual User object? Thank you. 回答1: Just pass it into the constructor and store it as an instance variable: class MyForm(forms.Form): def __init__(self, *args, **kwargs): self.request = kwargs.pop("request") super(MyForm, self).__init__(

django forms exclude values from choicefield

流过昼夜 提交于 2020-01-17 14:04:10
问题 I have the following model class ActionConfirm(models.Model): CONFIRM_METHOD = ( (u'ce', u'Certificate'), (u'tf', u'Trainee Feedback'), (u'ms', u'Multi Source Feedback'), (u'rp', u'Reflection upon Practice'), (u'ot', u'Other - Please add/describe') ) confirm_method = models.CharField(max_length=2, choices=CONFIRM_METHOD) user = User and the following form class ActionConfirmForm(forms.ModelForm): class Meta: model = ActionConfirm and I know that I can get their current choices by doing

django forms exclude values from choicefield

主宰稳场 提交于 2020-01-17 14:03:25
问题 I have the following model class ActionConfirm(models.Model): CONFIRM_METHOD = ( (u'ce', u'Certificate'), (u'tf', u'Trainee Feedback'), (u'ms', u'Multi Source Feedback'), (u'rp', u'Reflection upon Practice'), (u'ot', u'Other - Please add/describe') ) confirm_method = models.CharField(max_length=2, choices=CONFIRM_METHOD) user = User and the following form class ActionConfirmForm(forms.ModelForm): class Meta: model = ActionConfirm and I know that I can get their current choices by doing

Django raise forms.ValidationError not working

不问归期 提交于 2020-01-17 01:36:48
问题 I am trying to add a validator to django model form such that if specific value is selected then other field in the form should be entered if not entered it should give a validation error in the below form if the user selects "Project Support Activities" from the activity_name drop down then the project id field should be mandatory Django Form class ActivityTrackerModelForm(forms.ModelForm): date = forms.DateField(label='', widget=forms.DateInput(attrs={ "placeholder": "Select Date", 'id':

Django - multiple models in one page

谁说我不能喝 提交于 2020-01-16 20:58:35
问题 I have a model that looks like this: models.py class BHA_List(models.Model): well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list') bha_number = models.CharField(max_length=100) class BHA_Drill_Bit(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_drill_bit') bit_type = models.CharField(max_length=111) class BHA_overall(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall') drill_str_name = models

Django - multiple models in one page

不羁的心 提交于 2020-01-16 20:57:48
问题 I have a model that looks like this: models.py class BHA_List(models.Model): well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list') bha_number = models.CharField(max_length=100) class BHA_Drill_Bit(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_drill_bit') bit_type = models.CharField(max_length=111) class BHA_overall(models.Model): bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall') drill_str_name = models

django default Authenticaiton form shows username rather than email

試著忘記壹切 提交于 2020-01-16 09:48:06
问题 I implemented my own user login form with django like below from django.contrib.auth.forms import AuthenticationForm class CustomUserLoginForm(AuthenticationForm): class Meta: model = CustomUser fields = ('email', 'password') then as a view this is what I have: from rest_auth.views import LoginView from users.forms import CustomUserLoginForm class CustomLoginView(LoginView): def get(self, request): form = CustomUserLoginForm() return render(request, "api/test_template.html", context={"form":

How to get multiple files from a django FileField after model form object has been saved when form.save() does not return object

旧巷老猫 提交于 2020-01-16 08:43:12
问题 I have a django form class that extends django-postman's WriteForm (which is a ModelForm) with an additional field to upload some files. from postman.forms import WriteForm class MyWriteForm(WriteForm): file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True})) However, I need the saved model before I can do anything with the files. If I follow the example in the docs, I can extend postman's FormView and overwrite the save() method: from postman.views import

Django Email Change Form Setup

為{幸葍}努か 提交于 2020-01-16 08:43:11
问题 My email change form for users works, but I feel like my code is not written correctly. If I did it the way I have done below, I'd need a thousand else statements so that the page would return a response. Can someone tell me how I can make this more efficient/better? I'm not sure of the conventional way to do this Views.py def email_change(request): form = Email_Change_Form() if request.method=='POST': form = Email_Change_Form(request.POST) if form.is_valid(): if request.user.is_authenticated