1

Here is my urls.py of app 'api' of project 'REST Api':

from django.conf.urls import url
from . import views
app_name = 'api'
urlpatterns  = [
    url(r'^view/<int:test_id>/$' , views.add_task_to_list, name= "add_task"),
    url(r'^$', views.index, name=  "index")
]

and here is my views.py of app 'api':

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from .forms import task_form, list_form
from .models import List, Task

# Create your views here.
def index(request):
    l_form = list_form(request.POST or None)
    if l_form.is_valid():
        l_form.save()
        return HttpResponseRedirect('/')
    # tasks = Tasks()
    lists = List.objects.all()
    context = {
        'l_form': l_form,
        'lists': lists
    }
    return render(request, 'api/main.html', context)


def add_task_to_list(request, test_id):
    task = Task()
    task.list = List.objects.get(pk=test_id)
    form = task_form(request.POST or None, instance= task)
    if form.is_valid:
        form.save()
    context = {
        'task':task,
        'form':form
    }
    return render(request, "api/create_task.html", context)

And here is my models.py of app 'api':

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models

class List(models.Model):
    date = models.DateField()
    author = models.CharField(
        max_length=100,
        blank= True
    )
    def __str__(self):
        return self.author

class Task(models.Model):
    task_name = models.CharField(
        max_length=150,
    )
    complete_status = models.BooleanField(
        default= False,
        blank= True
    )
    priority = models.IntegerField()
    list = models.ForeignKey(List, on_delete=models.CASCADE)
    def __str__(self):
        return self.task_name

But when I run my server, and then navigate to 'views/5' or any such dynamic url as mentioned in 'urls.py', I get an error saying:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/view/5
Using the URLconf defined in REST_Api.urls, Django tried these URL patterns, in this order:

^admin/
^view/<int:test_id>/$ [name='add_task']
^$ [name='index']
The current path, view/5, didn't match any of these. 

Can somebody tell me what is the mistake I am committing here?

Ujjwal Kirti
  • 21
  • 1
  • 5

1 Answers1

0

This worked for me
from django.urls import path
path('view/<int:test_id>/' , views.add_task_to_list, name= "add_task"),

im using django 2.2 ref :https://docs.djangoproject.com/ en/3.1/topics/http/urls/