我在index.html
我的自定义联系表单中,如下所示:
<!-- FORM -->
<form role="form">
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" id="name" placeholder="Nombre">
<input type="email" class="form-control" id="email" placeholder="Email">
<input type="text" class="form-control" id="subject" placeholder="Tema">
</div>
<div class="col-md-8">
<textarea class="form-control" id="message" rows="25" cols="10" placeholder=" Texto de su mensaje..."></textarea>
<button type="button" class="btn btn-default submit-btn form_submit">Enviar mensaje</button>
</div>
</div>
</form>
<!-- END FORM -->
在urls.py
文件中,我有以下内容:
from django.conf.urls import include, url
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.contrib.auth.views import login
from django.contrib.auth.views import logout
from django.core.urlresolvers import reverse
#import createview which is the generic view for forms + validation
from django.views.generic.edit import CreateView
#import the user creation form
from django.contrib.auth.forms import UserCreationForm
from cfdipanel import forms
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^suscripcion$', views.suscripcion),
url(r'^escritorio$', views.report_list),
url(r'^report/(?P<pk>[0-9]+)/$', views.report_detailh, name='report_detailh'),
url(r'^login$', auth_views.login, {'template_name':
'cfdipanel/login.html'}, name='login'),
url(r'^logout/$', auth_views.logout, {'next_page': '/login'},
name='logout'),
url(r'^admin/', admin.site.urls),
url(r'^profile$', views.profile),
url(r'^registro$', views.registro, name='registro'),
]
如何在索引中创建相同的表单?那只是发给我一个发送的javascript警报,谢谢
答案 0 :(得分:0)
首先选择要使用的方法,通常是post,以及应该重定向的内容,例如此<form method="post" action="">
将重定向到同一页面,如果您希望警报在您的上下文中使用var,则: / p>
if request.method == 'POST':
#save form
context = {'alert':'alert'}
return render(request, 'your_template.html', context)
并在您的模板中:
<head>
....
{% if alert %}
<script type="text/javascript">alert('Form save')</script>
{% endif %}
</head>
答案 1 :(得分:0)
我是在我的 forms.py 中这样做的:
forms.py:
class ContactForm(forms.ModelForm):
class Meta():
model=Contact
fields = ['name','email','subject','message']
widgets = {
"name":forms.TextInput(attrs={'class':'form-control', 'id':"name"}),
"email":forms.EmailInput(attrs={'class':'form-control', 'id':"email"}),
"subject":forms.TextInput(attrs={'class':'form-control', 'id':"subject"}),
"message": forms.Textarea(attrs={'class':'form-control', 'id':"message", 'row':'3'})
}
在我的 html 文件中:
<form method='POST'>
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Your Name </label>
{{form.name}}
</div>
<div class="form-group col-md-6">
<label for="email">Your Email *</label>
{{form.email}}
</div>
</div>
<div class="form-group">
<label for="subject">Your Subject</label>
{{form.subject}}
</div>
<div class="form-group mb-3">
<label for="message">Your message *</label>
{{form.message}}
</div>
但最好的方法是here