Django form edit problem at intiliazation - django

I have a trouble with pre-population of the forms in Django. Because debuggin tool in Aptana doesn't work well, I couldn't detect the error.
I think when I try form = MemberSettings(default_data) form tries to validate the data and it gives errors for invalid fields even in first load.
Could you help me to find out the problem ?
Thanks
forms.py
class MemberSettings(forms.Form):
email = forms.EmailField(label=_("member_Email"),required = True)
currentPassword = forms.CharField(label=_("member_currentPassword"),widget=forms.PasswordInput,required=False)
newPassword = forms.CharField(label=_("member_newPassword"),widget=forms.PasswordInput,required=False)
newPasswordRe = forms.CharField(label=_("member_newPasswordRe"),widget=forms.PasswordInput,required=False)
emailPreference = forms.ChoiceField(label=_("member_email_preference"), widget=forms.RadioSelect(renderer=HorizRadioRenderer),choices = UserMailPreference.USER_MAIL_PREF,required = True)
gender = forms.ChoiceField(label=_("member_gender"), widget=forms.RadioSelect(renderer=HorizRadioRenderer),choices = UserGender.USER_GENDER ,required = False)
birthYear = forms.ChoiceField(label=_("member_birthyear"),required = False)
education = forms.ChoiceField(label=_("member_education"),choices = UserEducation.USER_EDU, required = False)
def __init__(self,*args, **kwargs):
super(MemberSettings,self).__init__(*args, **kwargs)
now = datetime.datetime.now()
birthYearList = []
for i in reversed(range (now.year-80,now.year-13)):
birthYearList.append((i,str(i)))
self.fields["birthYear"].choices = birthYearList
def clean_currentPassword(self):
field_data = self.cleaned_data['currentPassword']
if len(field_data) <= 0:
return field_data
if len(field_data.split(' ')) != 1:
raise forms.ValidationError(_('member_err_password_empty_char'))
if len(field_data) > 32:
raise forms.ValidationError(_('member_err_password_maxChar'))
if len(field_data) < 5:
raise forms.ValidationError(_('member_err_password_minChar'))
return field_data
def clean_newPassword(self):
field_data = self.cleaned_data['newPassword']
if len(field_data) <= 0:
return field_data
if len(field_data.split(' ')) != 1:
raise forms.ValidationError(_('member_err_password_empty_char'))
if len(field_data) > 32:
raise forms.ValidationError(_('member_err_password_maxChar'))
if len(field_data) < 5:
raise forms.ValidationError(_('member_err_password_minChar'))
return field_data
views.py
#login_required
def settings_edit(request):
u = request.user
if request.method == 'POST':
form = MemberSettings(request.POST)
if form.is_valid():
return redirect('/member/settings_edit/')
else:
return render_response(request, 'member/settings_edit.html', {'form': form})
else:
default_data = {
'email': u.email,
'gender': u.get_profile().gender,
'education': u.get_profile().education,
'birthYear': u.get_profile().birthyear,
'emailPreference': u.get_profile().mail_preference,
}
form = MemberSettings(default_data)
return render_response(request, 'user/settings_edit.html', {'form': form})

You need to use the initial argument when constructing your form instance in the view:
form = MemberSettings(initial=default_data)
See https://docs.djangoproject.com/en/1.3/ref/forms/api/ for more information. Hope that helps you out.

Related

Django - when 1 user accesses the same view with different url kwargs - why am I getting cross over of data?

I have a django app that contains samples. On my home page, it displays a table with many samples, containing hyperlinks to the 'Sample page' - which is a view get request.
When I click on several of these samples in tandem, to open new tabs, each to a specific tab - I am getting cross over of data - I.e the url sample_id kwargs is different, but the page is displaying the same results which is incorrect. When i refresh the page, the correct sample data appears.
Is there any way around this happening is a user is to open several different sample tabs at the same time? This would impact on results and could cause errors in the workflow offered by the django app.
*is this because my view is processing too much, so the different view request=s ends up over lapping ?
Edit: adding view:
class FilterVariantSampleView(ReportView, ReportPDFGenerator, FileGenerator, limsQuery):
template_name = os.path.join(
'results',
'filter_templates',
'sample_page.html'
)
type = 'sample'
choice = False
group_required = ['filter']
def get(self, request, *args, **kwargs):
self.user = request.user
self.obtain_sample_information(kwargs)
self.obtain_patient_information()
self.obtain_header()
# create sample notes form
sample_notes_form = SampleNotesForm(
instance=self.sample_obj)
self.context['sample_notes_form'] = sample_notes_form
self.create_variant_filter_formset()
panel_list = [o.run_id.machine_panel_id.panel_id
for o in self.all_sr_obj]
if len(panel_list):
self.panel_obj = panel_list[0]
# self.generate_hotspot_form()
self.assess_fails()
crs_qs = ClinicallyReportedSample.objects.filter(
sample_id=self.sample_obj)
crs_qs = crs_qs.exclude(
reported_on_panel__parent_panel__isnull=False,
primary_technical_report=False,
final_technical_report=False
)
remove_report_form_list = []
lims_molecular_results = self.query_lims_clone_molecular(
qs=crs_qs)
mol_df = lims_molecular_results[0]
summary = lims_molecular_results[1]
self.context['summary'] = summary
if crs_qs.count() == 1:
crs_obj = crs_qs[0]
self.context['crs_qs'] = crs_qs
remove_report_form = RemoveDiagnosticReportForm(
instance=crs_obj)
remove_report_form_list.append(remove_report_form)
else:
messages.add_message(self.request, messages.WARNING,
'This sample has been sequenced on multiple panels.')
self.context['crs_qs'] = crs_qs
lims_molecular_results = self.query_molecular_lims(
sample_obj=self.sample_obj)
for crs_obj in crs_qs:
remove_report_form = RemoveDiagnosticReportForm(
instance=crs_obj)
remove_report_form_list.append(remove_report_form)
if crs_obj.diagnostic_report_required == False:
messages.add_message(self.request, messages.WARNING,
f'This sample does NOT require a diagnostic reoprt '
f'for the {crs_obj.reported_on_panel} panel.')
self.context['remove_report_form_list'] = remove_report_form_list
return render(request, self.template_name, self.context)
def post(self, request, *args, **kwargs):
"""
"""
self.define_variant_filter_formset()
submit = request.POST.get('submit', None)
if submit:
if submit == 'Modify sample':
logger.info('')
logger.info('Sample: {}'.format(self.sample_obj))
logger.info("Submit: '{}'".format(submit))
# pass POST dict to formset
modify_formset = self.CRVFormSet(request.POST, prefix='crv')
# validate
if modify_formset.is_valid():
logger.info('Modify Variant Formset valid')
logger.info('')
self.modify_variants(modify_formset)
self.modify_sample()
return HttpResponseRedirect(
reverse('results:filter_variant_sample',
kwargs={'sample_id': self.sample_obj.id}))
else:
msg = ('Modify ClinicallyReportedVariant '
f'Formset NOT valid: {modify_formset.errors}')
logger.error(msg)
messages.add_message(request, messages.ERROR, msg)
return HttpResponseRedirect(
reverse('results:filter_variant_sample',
kwargs={'sample_id': self.sample_obj.id}))
elif submit == 'Update':
form = RemoveDiagnosticReportForm(request.POST,
instance=ClinicallyReportedSample.objects.get(
sample_id=kwargs['sample_id'],
reported_on_panel=request.POST['reported_on_panel'])
)
if form.is_valid():
logger.info('RemoveDiagnosticReportForm is valid')
try:
form.save()
except Exception as e:
logger.error(e)
else:
msg = f'RemoveDiagnosticReportForm is NOT valid: {form.errors}'
logger.error(msg)
messages.add_message(self.request, messages.ERROR, msg)
return HttpResponseRedirect(request.path_info)
elif 'Download' in submit:
panel_name = submit.split(' ')[1]
self.panel_obj = Panel.objects.get(panel_name=panel_name)
self.crs_obj = ClinicallyReportedSample.objects.get(
sample_id=self.sample_obj, reported_on_panel=self.panel_obj)
if 'panel data' in submit:
logger.info(f'Downloading data for {self.sample_obj}')
if self.crs_obj.sample_id.external_dept.site in ['Newcastle', 'Sheffield']:
downloaded_data = self.download_yne_data()
if downloaded_data:
self.crs_obj.downloaded_data = True
self.crs_obj.downloaded_data_user = request.user
self.crs_obj.downloaded_data_date = timezone.now()
self.crs_obj.save()
return downloaded_data
else:
return HttpResponseRedirect(self.request.path_info)
elif 'report' in submit:
logger.info(f'Downloading report for {self.sample_obj}')
# update crs_obj
self.crs_obj.download_technical_report = True
self.crs_obj.download_technical_report_user = self.user
self.crs_obj.download_technical_report_date = timezone.now()
self.crs_obj.save()
# return HttpResponseRedirect(self.request.path_info)
lims_molecular_results = self.query_molecular_lims(
sample_obj=self.sample_obj)
mol_df = lims_molecular_results[0]
summary = lims_molecular_results[1]
if mol_df.empty:
logger.info('No molecular tests done')
self.latex_context['gene_status'] = False
else:
gene = mol_df[
(mol_df['test_name'] == 'gene') &
(mol_df['status'] == 'complete')
]
if gene.empty:
self.context['gene'] = False
else:
msg = ("gene Sanger in-fill test has been completed.{}")
result = gene['result'].to_string(index=False)
if result == 'normal or wild-type':
msg = msg.format(' No detectable variants.')
msg_info = messages.INFO
gene_df = pd.DataFrame()
elif result == 'failed':
msg = msg.format(' Unfortunately the test failed.')
msg_info = messages.WARNING
gene_df = pd.DataFrame()
elif result == 'mutated' or result == 'suspicious':
gene_df = self.get_gene_result(gene, technical_report=True)
if result == 'suspicious':
msg = msg.format(' There is suspicion of a variant '
'(see below for more information).')
else:
msg = msg.format(' A variant was detected '
'(see below for more information).')
msg_info = messages.INFO
else:
msg = msg.format(' Error - contact administrator.')
msg_info = messages.ERROR
result = 'error'
gene_df = pd.DataFrame()
self.latex_context['gene_status'] = result
self.latex_context['gene_df'] = gene_df
logger.info(msg)
messages.add_message(self.request, msg_info, msg)
print(self.latex_context)
# call method from TechnicalReportPDFGenerator
self.generateReport(report_type='technical')
response = self.download_technical_report()
return response
return render(request, self.template_name, self.context)
This may be due to the cache of your browser, are you sure the calls are going through the first time ?

I am getting object has no attribute update in my Django Website

I have a crud application and would like to update the items. I have checked some solutions online which explains that the .update method can't be used like this but for only a queryset. I don't know how to update the information manually. Thanks
views.py
def UpdateReservation(request, pk):
table_exists = Reservation.objects.get(id=pk)
form = ReservationForm(instance=table_exists)
if request.method == "POST":
if request.POST['table']:
request.POST = request.POST.copy()
table_exists = Reservation.objects.get(id=pk)
try:
if table_exists:
time = form['time']
people = form['people']
comment = form['comment']
date_reserved = form['date_reserved']
email = form['email']
phone = form['phone']
first_name = form['first_name']
resrv = table_exists.update(email=email, first_name=first_name, people=people, time=time, date_reserved=date_reserved, comment=comment, table=table_exists)
resrv.save()
messages.success(request, "you have successfully edited.")
return redirect(request.path)
else:
messages.error(request, "Unable to edit.")
return redirect(request.path)
except Exception as e:
messages.error(request, "Unknown error" + str(e))
return redirect(request.path)
context = {"form":form}
return render(request, "dashboard/super/admin/update_reserve.html", context)
After trying that, it returns the error, Unknown error'Reservation' object has no attribute 'update'
It is better to validate the form and update the individual fields with respective values and then save the object. The view should be as follows:
from django.shortcuts import get_object_or_404
def UpdateReservation(request, pk):
table_exists = get_object_or_404(Reservation, id=pk)
form = ReservationForm(instance=table_exists)
if request.method == "POST":
form = ReservationForm(request.POST, instance=table_exists)
if form.is_valid():
time = form['time']
people = form['people']
comment = form['comment']
date_reserved = form['date_reserved']
email = form['email']
phone = form['phone']
first_name = form['first_name']
table_exists.email = email
table_exists.first_name = first_name
table_exists.people = people
table_exists.time = time
table_exists.date_reserved = date_reserved
table_exists.comment = comment
table_exists.save()
messages.success(request, "you have successfully edited.")
return redirect(request.path)
else:
messages.error(request, "Unable to edit.")
return redirect(request.path)
context = {"form": form}
return render(request, "dashboard/super/admin/update_reserve.html", context)
You should use model like this:
table_exists = Reservation.objects.get(id=pk)
table_exists.email = email
table_exists.first_name = first_name
table_exists.people = people
table_exists.time = time
table_exists.date_reserved = date_reserved
table_exists.comment = comment
table_exists.table = table_exists
table_exists.save()

How I can reduce logic of hande form in next Django view?

I'm studing a django course right now. Teacher showed how find short route with ascross points by graph algorithm. He writed many code in view, I think it's some dirty. I shorten piece of code by using session in RouteSession class. But there is still many code.
Should do any thing in this case? Thanks.
def find_routes(request):
if request.method == "POST":
form = RouteForm(request.POST or None)
if form.is_valid():
data = form.cleaned_data
from_city = data['from_city']
to_city = data['to_city']
across_cities_form = data['across_cities']
travel_time = data['travel_time']
graph = get_graph()
all_ways = list(dfs_paths(graph, from_city.id, to_city.id))
if len(all_ways) == 0:
messages.error(request, 'There is no routes')
return render(request, 'routes/home.html', {'form': form})
if across_cities_form:
across_cities = [city.id for city in across_cities_form]
right_ways = []
for way in all_ways:
if all(point in way for point in across_cities):
right_ways.append(way)
if not right_ways:
messages.error(request, 'The route through these cities is impossible')
return render(request, 'routes/home.html', {'form': form})
else:
right_ways = all_ways
trains = []
for route in right_ways:
tmp = {'trains': []}
total_time = 0
for i in range(len(route) - 1):
qs = Train.objects.filter(from_city=route[i], to_city=route[i + 1])
qs = qs.order_by('travel_time').first()
total_time += qs.travel_time
tmp['trains'].append(qs)
tmp['total_time'] = total_time
if total_time <= int(travel_time):
tmp['from_city'] = from_city
tmp['to_city'] = to_city
trains.append(tmp)
if not trains:
messages.error(request, 'Travel time is longer than you're looking for')
return render(request, 'routes/home.html', {'form': form})
trains = sorted(trains, key=lambda x: x['total_time'])
routes = SessionRoute(request)
routes.clear()
routes.add(trains)
cities = {'from_city': from_city.name, 'to_city': to_city.name}
context = {'form': RouteForm(), 'routes': routes, 'cities': cities}
return render(request, 'routes/home.html', context)
return render(request, 'routes/home.html', {'form': form})
else:
messages.error(request, 'Please create a route')
form = RouteForm()
return render(request, 'routes/home.html', {'form': form})
Thanks for comment.
I moved logic to form class, and reloaded clean() method:
def clean(self, *args, **kwargs):
from_city = self.cleaned_data.get('from_city')
to_city = self.cleaned_data.get('to_city')
across_cities_data = self.cleaned_data.get('across_cities')
travel_time = self.cleaned_data.get('travel_time')
graph = get_graph()
all_ways = list(dfs_paths(graph, from_city.id, to_city.id))
if len(all_ways) == 0:
raise forms.ValidationError('Маршрута, удовлетворяющего условиям не существует.')
if across_cities_data:
across_cities = [city.id for city in across_cities_data]
right_ways = []
for way in all_ways:
if all(point in way for point in across_cities):
right_ways.append(way)
if not right_ways:
raise forms.ValidationError('Маршрут через эти города невозможен')
else:
right_ways = all_ways
trains = []
for route in right_ways:
tmp = {'trains': []}
total_time = 0
for i in range(len(route) - 1):
qs = Train.objects.filter(from_city=route[i], to_city=route[i + 1])
qs = qs.order_by('travel_time').first()
total_time += qs.travel_time
tmp['trains'].append(qs)
tmp['total_time'] = total_time
if total_time <= int(travel_time):
tmp['from_city'] = from_city
tmp['to_city'] = to_city
trains.append(tmp)
if not trains:
raise forms.ValidationError('Время в пути больше заданного')
self.cleaned_data['trains'] = trains
return super(RouteForm, self).clean()
It works great!

Django Form Wizard: Why is this happening?

I have a dinamic form wizard step as following:
class AltaForm6(forms.Form):
CHOICES = ((1, 1,), (2, 2,))
username = ChoiceFieldInvalid(required=True, label="Usuario", widget=forms.RadioSelect, choices=CHOICES)
username2 = forms.CharField(required=False, label="Otro")
#username = ChoiceWithOtherField(required=True, label="Usuario", choices=CHOICES)
def clean_username2(self):
username = self.cleaned_data.get('username')
username2 = self.cleaned_data.get('username2')
if username == "Otro" and len(username2) == 0:
raise forms.ValidationError("Debe especificar un nombre de usuario")
return username2
def clean_username(self):
username = self.cleaned_data.get('username')
return username
Then i dynamically change the choices values:
class AltaWizard(SessionWizardView):
template_name = 'agroshare/wizard.html'
def get_form(self, step=None, data=None, files=None):
form = super(AltaWizard, self).get_form(step, data, files)
if step == '5':
import logging
logger = logging.getLogger(__name__)
cleaned_data = self.get_cleaned_data_for_step('2') or {}
logger.error(cleaned_data)
nombre = cleaned_data.get('nombre')
apellido = cleaned_data.get('apellido')
first = possibleUid(nombre, apellido, '1')
second = possibleUid(nombre, apellido, '2')
if ' ' in nombre:
third = possibleUid(nombre, apellido, '3')
form.fields['username'].choices = ((first, first,), (second, second,), (third, third,), ("Otro", "Otro",))
if ' ' in apellido:
fourth = possibleUid(nombre, apellido, '4')
form.fields['username'].choices = ((first, first,), (second, second,), (fourth, fourth,), ("Otro", "Otro",))
else:
form.fields['username'].choices = ((first, first,), (second, second,), ("Otro", "Otro",))
if step == '5':
form.user = self.request.user
return form
def render(self, form=None, **kwargs):
form = form or self.get_form()
context = self.get_context_data(form=form, **kwargs)
if self.steps.current == '5':
form2 = self.get_form('5')
cleaned_data = self.get_cleaned_data_for_step('5') or {}
username = cleaned_data.get('username')
form2.fields['username'].choices = [username]
return self.render_to_response(context)
The problem is, when i go back to this step trough the wizard, it does not modify the "choices" values, it shows "CHOICES = ((1, 1,), (2, 2,))"
How can i achieve that when i go back to this step, the form can actually show the values i want?

Access missing value in form.cleaned_data

I was trying to dynamically generate fields as shown in http://jacobian.org/writing/dynamic-form-generation/. My case slightly differs in that I am looking to use multiplechoicefield that is dynamically created. This is what I came up with...
views.py
def browseget(request):
success = False
if request.method == 'POST':
list_form = ListForm(request.POST)
if list_form.is_valid():
success = True
path = list_form.cleaned_data['path']
minimum_size = list_form.cleaned_data['minimum_size']
follow_link = list_form.cleaned_data['follow_link']
checkboxes = list_form.cleaned_data['checkboxes']
....do something
else:
list_form = ListForm(name_list)
ctx = {'success': success, 'list_form': list_form, 'path': path, 'minimum_size': minimum_size}
return render_to_response('photoget/browseget.html', ctx, context_instance=RequestContext(request))
forms.py
class ListForm(forms.Form):
path = forms.CharField(required=False)
minimum_size = forms.ChoiceField(choices=size_choices)
follow_link = forms.BooleanField(required=False, initial=True)
def __init__(self, *args, **kwargs):
name_list = kwargs.pop('name_list', None)
super(ListForm, self).__init__(*args, **kwargs)
print 'Received data:', self.data
if name_list:
name_choices = [(u, u) for u in name_list]
self.fields['checkboxes'] = forms.MultipleChoiceField(required=False, label='Select Name(s):', widget=forms.CheckboxSelectMultiple(), choices=name_choices)
def clean_path(self):
cd = self.cleaned_data
path = cd.get('path')
if path == '': path = None
return path
def clean_minimum_size(self):
cd = self.cleaned_data
minimum_size = cd.get('minimum_size')
if minimum_size is None: minimum_size = 0
return int(minimum_size)
The form generates and displays perfectly... until I post some data. The 'checkboxes' field doesn't show up in list_form.cleaned_data.items() while it shows in self.data. As it is the form breaks with a KeyError exception. So Im asking, how do i access the checkboxes data?
You're not passing in the name_list parameter when you re-instantiate the form on POST, so the field is not created because if name_list is False.