Answer the question
In order to leave comments, you need to log in
How to insert data from html into database?
I have a calendar in html and an ok button. and I wanted the user to select a date and it was inserted into the database (I use sqlite). I created a form
class RequestForm(forms.Form):
date = forms.DateField(widget=forms.widgets.DateInput(format="%d/%m/%Y"))
def index(request):
return render(request, 'request/index.html',{'user' : request.user,})
def request(request):
if request.method == 'POST':
f = RequestForm(request.POST)
if f.is_valid():
date = f.cleaned_data['date']
new = RequestForm(date=request.post['date'])
new.save();
return HttpResponseRedirect(reverse('request.views.index'))
else:
f = RequestForm()
return render(request, 'request/request.html', {'f': f})
{% block main %}
<h1> request </h1>
<form name="form" method="post" action="{% url request.views.request %}"
{% csrf_token %}
{{ f.as_p }}
<input type = "submit" value = "ok"/>
</form>
{% endblock %}
{% block main %}
Welcome, {{user.username}}
{% endblock %}
<body>
<form>
<p>Выберите дату: <input type="date" name="calendar">
<input type="submit" value="ok"></p>
</form>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
def request(request): this is also epic, override the request...
I fixed it on the model, renamed the application to application. in application.html wrote
<form class="navbar-form contact_form" action="{% url 'application' %}" method="post">
<input type="date" name="calendar">
<input type="submit" value="ok"></p>
def application (request):
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question