2016-02-01 46 views
0

我做了一个HTML表单,其中自动建议选项也在城市领域。我想知道如何将html表单的值传递给django admin。如何将html表单中的值传递给Django管理员?

到现在为止,我制作了一个表单,其中名称,城市,电话号码,性别等字段在admin.py中注册。由此我不能直接通过django管理员注册。

+0

我不完全确定你在问什么?你是否想知道是否可以在django管理面板中注册一个HTML表单? –

回答

0

如果你已经有了你的html表单,你必须在views.py中创建你的函数。 在这个文件中,你必须编写接收你从html表单发送的数据的代码。

阅读文档:Django forms

你可能有一个HTML表单类似如下:

<form action="/your-name/" method="post"> 
    <label for="your_name">Your name: </label> 
    <input id="your_name" type="text" name="your_name" value="{{ current_name }}"> 
    <input type="submit" value="OK"> 
</form> 

可以在forms.py创建表单:

from django import forms 

class NameForm(forms.Form): 
    your_name = forms.CharField(label='Your name', max_length=100) 

您的功能看起来像这样(在views.py中)

from django.shortcuts import render 
from django.http import HttpResponseRedirect 

from .forms import NameForm 

def get_name(request): 
# if this is a POST request we need to process the form data 
if request.method == 'POST': 
    # create a form instance and populate it with data from the request: 
    form = NameForm(request.POST) 
    # check whether it's valid: 
    if form.is_valid(): 
     # process the data in form.cleaned_data as required 
     # ... 
     # redirect to a new URL: 
     return HttpResponseRedirect('/thanks/') 

# if a GET (or any other method) we'll create a blank form 
else: 
    form = NameForm() 

return render(request, 'name.html', {'form': form}) 
相关问题