Task Create Module
Steps to Develop a Task Create module.
1. Create template for Task Create module.
templates/todo/task_form.html
2. Create view function for Task Create module
Import Statements:
These statements import the necessary classes for creating a Task Create view and applying the LoginRequiredMixin.
Class Definition:
Here's a breakdown of the code:
TaskCreate
is a class-based view for creating tasks.It inherits from
LoginRequiredMixin
, which ensures that only logged-in users can access this view.The
model
attribute is set to theTask
model, indicating that this view is used for creating instances of theTask
model.The
fields
attribute specifies the fields from theTask
model that will be included in the form. In this case, it includes 'title', 'description', and 'complete'.The
success_url
attribute is set to the URL to redirect to after the form is successfully submitted. In this case, it's set to the URL named 'tasks' usingreverse_lazy
.The
form_valid
method is overridden to set theuser
field of the task to the current logged-in user before saving the form.self.request.user
represents the user who made the request.form.instance
represents the instance of the model form before it's saved.
This code ensures that a new task is associated with the currently logged-in user when it is created.
3. Mapping URLs to Task Create view.
Make sure you have the necessary URLs and other configurations in place to use this Task Create view. For example, in your urls.py
file, you might have something like:
4. Update the task_list.html
add href value to line no. 18 and 49 in task_list.html
5. Run the development server to check the functionality:
Start the development server if it's not running already:
Access the interface by visiting http://localhost:8000/task-create
in your web browser.
Output:
Last updated