varad
varad

Reputation: 8029

django saving form field in generic or functional view

Lets say I have a form and its files are user, value, age

I am using generic or functional view... so how can I do like

def SomeView(request):
    mod = MyModel()
    mod.user = request.user
    mod.value = form.cleaned_data["value"]
    mod.save()

I think mod.value = form.cleaned_data["value"] can only be done with class based view. But how can I do this in generic or functinal view.

Upvotes: 0

Views: 38

Answers (2)

wobbily_col
wobbily_col

Reputation: 11921

If you need to do it with functional views, you need to check the request.POST

def SomeView(request)

    c = RequestContext(request, {})
    if request.method == 'POST' :
        form = YourForm(request.POST or None  )

        # your code here, 


       # redirect on success probably  
    else :
       # create blank form 
      form = YourForm(None)

    c[form]=form      
    return render_to_response("path/to/your/templet.html", c)          

Upvotes: 1

inlanger
inlanger

Reputation: 2996

You can get value data from request(GET or POST) - request.GET.get('value', 'default_value') or request.POST.get('value', 'default_value')

Upvotes: 0

Related Questions