Django - 将数据从View传递到Db以允许在View中使用

时间:2013-08-07 00:44:52

标签: django forms post redirect django-forms

def Scan(request):
    form = SubmitDomain(request.POST or None) # A form bound to the POST data
    if request.method == 'POST': # If the form has been submitted...
        if form.is_valid(): # If form input passes initial validation...
            domainNmCleaned = form.cleaned_data['domainNm']  ## clean data in dictionary
            form.save() #save cleaned data to the db from dictionary
            try:
                return HttpResponseRedirect('/Processscan/?domainNm=' + domainNmCleaned)
            except:
                raise ValidationError(('Invalid request'), code='invalid')    ## [ TODO ]: add a custom error page here.
    else:
        form = SubmitDomain()

    return render(request, 'VA/index.html', {
        'form' : form
    })

def Processscan(request):
    EnteredDomain = request.get('domainNm', '')
    return HttpResponse("We got to the processor with domain: " + EnteredDomain)

请放轻松我 - 我还在学习:)。

我现在遇到GET的问题,当我使用POST时,我最初的域名请求 - 我得到了:

'WSGIRequest' object has no attribute 'get'

在:

EnteredDomain = request.get('domainNm', '')

1 个答案:

答案 0 :(得分:1)

你可以直接调用scanprocess,或者只是通过GET传递它:

if form.is_valid(): # If form input passes initial validation...
    domainNm = form.cleaned_data['domainNm']  ## clean data in dictionary
    form.save() #save cleaned data to the db from dictionary

    try:
        return HttpResponseRedirect('/Scanprocess/?domainNm=' + domainNm)
    except:
        raise ValidationError(_('Invalid domain name'), code='invalid')

然后在您的视图中,您可以通过request.GET.get('domainNm', '')

了解它

据我所知,你所需要的只是urls.py

url(r'^Scanprocess/$', name_of_view),