Hello, I'm using Django ModelForm on add/edit URLs which are both sharing a single template file. The code I'm working with is like this:
class WebappAuthorize(models.Model): institution_name = models.CharField(max_length=32, help_text="Enter your institution name") email = models.CharField(max_length=20, help_text="Example, t...@example.com", default=get_current_user(object(), 'email')) institution_logo = models.FileField(upload_to=api.file_upload('files', 'logo'), blank=True, help_text="Upload your institution logo") institution_url = models.CharField(max_length=50, blank=True, help_text="Example, http://example.com") phone = models.IntegerField(null=True, blank=True, help_text="Enter a phone number") class Meta: db_table = u'authorize' forms.py ------------ class AuthorizeForm(forms.ModelForm): class Meta: model = WebappAuthorize views.py ------------ def get_current_user(request, **kwargs): if kwargs['email']: # select email from User where request_email = User.objects.filter(email=request.user) email = request_email.values('email') edict = email[0] return edict.get('email') def authorize(request): request_user = User.objects.filter(username=request.user) username = request_user.values('username') udict = username[0] u = str(udict.get('username')) context = {'page_title' : 'Authorize your institution'} consumer_obj = WebappOauthConsumer.objects.filter(user=u) # get user_id from user print consumer_obj c = consumer_obj.values('user_id') for i in c: if i.get('user_id') == u: return redirect('update-authorize-institution', user=u, action='update') if request.method == 'POST': authorize_form = AuthorizeForm(request.POST, request.FILES) if authorize_form.is_valid(): print request.POST ckey = consumer.generate(request.POST.get('institution_name', '')) if ckey is not None: oauthconsumer_model = WebappOauthConsumer() oauthconsumer_model.user = User(u) oauthconsumer_model.institution_name = WebappAuthorize(request.POST.get(u'institution_name', '')) oauthconsumer_model.consumer_key = ckey.get('consumer_key') oauthconsumer_model.consumer_secret = ckey.get('consumer_secret') oauthconsumer_model.created = int(time.time()) oauthconsumer_model.changed = int(time.time()) oauthconsumer_model.save() return redirect('update-authorize-institution', user=u, action='update') else: print authorize_form.errors else: authorize_form = AuthorizeForm() context['form'] = authorize_form context['action'] = 'new' context['consumer'] = consumer_obj.values().order_by('id') for i in context['consumer']: p = i request.session['consumer'] = p return render_to_response("webapp/authorize.html", context, context_instance=RC(request)) And the edit view ------------------------ def edit_authorize(request, **kwargs): context = {'page_title' : 'Update Authorize your institution'} context['user'] = kwargs['user'] context['action'] = kwargs['action'] print request.session['consumer'] if request.method == 'POST': authorize_form = AuthorizeForm(request.POST, request.FILES) oauthconsumer_model.changed = date.today() oauthconsumer_model.save() else: u = WebappOauthConsumer.objects.get(user=kwargs['user']) # Make sure it returns a single user authorize_form = AuthorizeForm(data=request.POST, files=request.FILES, instance=u) # instance of WebAuthorize context['form'] = authorize_form return render_to_response("webapp/authorize.html", context, context_instance=RC(request)) In each of these form fields, I want their values to be set on edit. A fix I'm working with at the moment is to store the values in session variables, then print it on the input value property of each form field in the template file. {{ request.session.consumer.institution_name_id}} <div class="group"> <label for="{{ form.institution_name.name }}" class="label">{{ form.institution_name.label }}</label> {% if action == 'update' %} <input class="text_field" type="text" disabled="disabled" name="{{ institution_name_id }}" value="{{ request.session.consumer.institution_name_id }}" /> {% else %} <input class="text_field" type="text" name="{{ form.institution_name }}" value="{{ request.session.consumer.institution_name_id }}" /> {% endif %} {{ form.institution_name.help_text }} </div> <div class="group"> <label for="{{ form.email.name }}" class="label">{{ form.email.label }}</label> {{ form.email }} {{ form.email.help_text }} </div> For example, on the email field, I want its value prepopulated on edit. I tried using the default field option on the model, but there's no easy way of getting logged in user outside views.py. To provide a workaround, I created a function in views.py which takes the request object as argument, then I call the function in models.py like so: // in models.py from webapp.views import get_current_user email = models.CharField(max_length=20, help_text="Example, t...@example.com", default=get_current_user(object(), 'email')) But it seems impossible to import views.py into models.py. I'm getting: ImportError: cannot import name get_current_user Please, how can I get such functionality in my template file. Thanks -- Odeyemi 'Kayode O. http://www.sinati.com. t: @charyorde -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.