T
T
trytrytry2013-12-20 22:28:28
Django
trytrytry, 2013-12-20 22:28:28

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"))

and here is the view, but I don't know why the error occurs.
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})

while I just have a field in html is displayed
{% 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 %}

I don’t understand yet how to insert and bind a calendar to the jang. here is the calendar code
<body>
  <form>
   <p>Выберите дату: <input type="date" name="calendar">
   <input type="submit" value="ok"></p>
  </form>
 </body>
</html>

please help me figure this out

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
maxaon, 2013-12-21
@maxaon

You need a model. Read the documentation.

A
alternativshik, 2013-12-21
@alternativshik

def request(request): this is also epic, override the request...

T
trytrytry, 2013-12-21
@trytrytry

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>

Now I'm wondering how exactly to process in
def application (request):

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question