Tuesday, April 22, 2025

Django To-Do - Html Forms

You are advised to check corresponding YouTube video at the end of this article.

First, we will complete navigation in header.html:


<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>

  </head>
  <body>
    {% block navigation  %}
    <ul>
      <li><a href="{% url 'index' %}">Home</a></li>
      <li><a href="{% url 'add' %}">New Post</a></li>
      <li><a href="{% url 'admin' %}">Admin</a></li>
    </ul>
    {% endblock %}
    <hr>

Then, new route is needed in local urls.py file:


from django.urls import path
from .import views

urlpatterns = [
    path('', views.index, name='index'),
    path('details/<int:id>', views.details, name='details'),
    path('admin/', views.admin, name="admin"),
    path('add/', views.add, name='add'),
]

Generic space for new_post section in base.html:


{% include 'parts/header.html ' %}

{% block listing  %}
{% endblock %}

{% block details  %}
{% endblock %}

{% block new_post %}
{% endblock %}

{% include 'parts/footer.html ' %}

What about html form ? let's create add.html in templates directory:


{% extends 'base.html' %}

{% block new_post %}

<form action="{% url 'add' %}" method="post">
  {% csrf_token %}

  <label for="title">Title</label>
  <input type="text" name="title" id="title">
  <br><br>

  <label for="text">Text</label>
  <textarea name="text" rows="10" cols="80"></textarea>
  <br><br>

  <input type="submit" value="Submit">
</form>

{% endblock %}

Form is created, now we need to create a function in views.py file

Let's do that in next tutorial

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...