【问题标题】:Django - Pass Data from View to Db to allow use in ViewDjango - 将数据从 View 传递到 Db 以允许在 View 中使用
【发布时间】:2013-08-08 04:58:42
【问题描述】:
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)

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

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

'WSGIRequest' object has no attribute 'get'

开:

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

【问题讨论】:

    标签: django forms post redirect django-forms


    【解决方案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),
    

    【讨论】:

    • 这有安全隐患吗?
    • 我不知道。用户可以发送任何他想要的 GET 字符串。
    • 你应该处理 HttpResponseRedirect 异常。
    • 你能用一个简短的例子更新你的答案吗?我真的很感激
    • 另外,我怎样才能在 Scanprocess 视图中访问 domainNm 的值?
    猜你喜欢
    • 2019-06-15
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 2020-11-13
    • 2019-07-26
    相关资源
    最近更新 更多