def edit(request, name): account_info = Account.objects.get(account_name=name) id = account_info.id history = History.objects.get(account=id) data_dict = [history.history_name, history.saved_date] if request.method == 'POST': form = DataEditForm(request.POST) if form.is_valid(): account_name = form.cleaned_data['name'] return HttpResponseRedirect('/questions/5/' + account_name + '/success/') else: form = DataEditForm() context = {'form': form, 'name': data_dict[0], 'date': data_dict[1], 'href': '/questions/5/' + name} return render(request, 'accounts/data_form.html', RequestContext(request, context))
def edit(request, name): """View to handle model logic and return to success page. Link transports 'name' in edit(request, name).""" # Get the Account model record for 'name'. account_info = Account.objects.get(account_name=name) id = account_info.id # Retrieve the History (ForeignKey) for the account_name = name. If it doesn't exist history = []. history = account_info.history_set.all() # Build dictionary of history data for context, specifically the href to display compare.html for account_name. history_dict = {} for record in history: history_dict.update({record.history_name: {'name': record.history_name, 'date': record.saved_date, 'href': '/tasks/5/' + record.history_name + '/compare/'}}) # Handle the form's initial view and request.POST. if request.method == 'POST': # Instantiate the form class for the template. form = DataEditForm(request.POST) if form.is_valid(): # Using cleaned data update and save account_info. account_name = form.cleaned_data['name'] account_info.account_name = account_name account_info.save() # Get the current time and update the History model with new entry. now = datetime.datetime.now() account_info.history_set.create(account=account_name, history_name=name, saved_date=now) # Return to a success page using TemplateView.as_view(). return HttpResponseRedirect('/tasks/5/' + account_name + '/success/') else: form = DataEditForm() # href for the form - action = href1 href1 = '/tasks/5/' + name + '/' context = {'form': form, 'history': history_dict, 'href1': href1, 'head_title': 'AcisTek | Data', 'head_alert': ' ', 'body_title': 'Edit Data', 'footer_title': 'History Data'} return render(request, 'accounts/data_form.html', RequestContext(request, context))