Tutorial 4: Django User Input and Templates

Posted by

Django User Input and Templates | Tutorial 4

Django User Input and Templates

In this tutorial, we will learn how to handle user input and use templates in Django.

User Input

Handling user input is a crucial aspect of web development. In Django, we can easily capture user input using forms. Here’s a simple example of how to create a form in Django:

Templates

Templates are used to create dynamic HTML content in Django. By using templates, we can separate our HTML code from our Python code, making our application more organized and maintainable. Here’s an example of how to use templates in Django:

Create a new HTML file called submit_success.html and add the following content:

<!DOCTYPE html>
<html>
<head>
  <title>Submission Successful</title>
</head>
<body>
  <h1>Thank you for submitting the form!</h1>
</body>
</html>

In your Django view function, render this template when the form is successfully submitted:

from django.shortcuts import render

def submit_form(request):
    if request.method == 'POST':
        # Process the form data
        return render(request, 'submit_success.html')

Now, when the form is successfully submitted, the user will see the “Submission Successful” message.

That’s it! You have now learned how to handle user input and use templates in Django. Feel free to explore more advanced features of Django to enhance your web development skills.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@techlab4
4 months ago

Don't forget to like and subscribe !!
Thanks for watching! <3