0

I need to pass in the name of the user, so that I can set the user_name in the service model in the html. Should I even be setting default values in the html? If not, how would I do this in the view? I don't want the username field to appear in the form (I've already taken care of that) I want it to be updated in the background when the user clicks the 'create service' button.

here is my views.py file:

def create(request):
    # val= 3
    if request.POST:
        form= ServiceForm(request.POST)
        if form.is_valid():
            # form.fields['zipcode'].initial = val
            service_obj = form.save()
            return render_to_response('services/service_created.html', 
                              {'service': Service.objects.get(id=service_obj.id)})
    else:
        form = ServiceForm()

    args= {}
    args.update(csrf(request))
    args['form'] = form
    active_user = request.session['active_user'] = request.user.username

    #The line below doesn't work how I would like it to
    return render_to_response('services/create_service.html', {'args': args, 'user': active_user} )

Here is my create_service.html

<form action="/services/create" method="post" enctype="multipart/form-data">{% csrf_token %}
<ul>
{{form.as_p}}
</ul>

<input type="submit" name="submit" value="Create Service">
</form>
Michael Smith
  • 3,307
  • 5
  • 25
  • 43
  • 1. [use `render` instead of `render_to_response`](http://stackoverflow.com/questions/21371005/django-why-should-i-ever-use-the-render-to-response-at-all) 2. you're passing `active_user` as `{{ user }}` but you're not using it anywhere, and your explanation is unclear - what's the problem? What is the expected output \ what are you trying to achieve? – yuvi Sep 24 '14 at 22:28
  • Ultimately when the user clicks the submit button, it should update the Services table and have the user_name set to the user that created the service (the logged in user). The user_name field will not appear in the form html; it should be updated in the background – Michael Smith Sep 24 '14 at 23:10

2 Answers2

2

Maybe this is what you want

service_obj = form.save(commit=False)
service_obj.user_name = request.user.username
service_obj.save()

Since your html never displays the user field, you should just not even include it in your render_to_response call(). This view is certainly the best place for doing this. You don't want (or need) the form to submit the username info when you can just get it from the request before you save your model to the db.

Al W
  • 7,539
  • 1
  • 19
  • 40
  • One more thing, if you have a sec. If I did want to pass a variable in addition to args, how would I do that? The way I was doing it above doesn't seem right – Michael Smith Sep 24 '14 at 23:56
  • The way you were doing it is perfectly ok. my code usually looks like render_to_response('file.html', {var1:'stuff', var2:'morestuff', form=form}) – Al W Sep 25 '14 at 01:09
  • The way I got it to work was that I passed in args as 'args':args['form']. Then the html above worked fine. For some reason 'args': 'args' wasn't rendering the form properly with additional fields to render_to_response. In any case, it's working now :) – Michael Smith Sep 25 '14 at 17:51
0

I believe what you're looking for is:

 form = ServiceForm(initial={'user_name': varThatHoldsUsername})

Documentation here.

Joseph
  • 12,678
  • 19
  • 76
  • 115