madhu yadav
madhu yadav

Reputation: 43

How to call view in another app using template tags in django?

I am Getting an error Reverse for 'question_list' not found. 'question_list' is not a valid view function or pattern name. after using the template tag 'collab_app:question_list' in template.

home.html:

{% extends "_base.html" %} 
{% load static %}
{% load socialaccount %}
{% block title %}Home{% endblock title %} 
{% block content %}
<h1>Homepage</h1>
<a href="{% url 'collab_app:question_list' %}">Ask Question Here</a>
<img class="collabimage" src="{% static 'images/collab.jpg' %}" ><br>
{% if user.is_authenticated %}
  Hi {{ user.email }}
  <p><a href="{% url 'account_logout' %}">Log Out</a></p>
{% else %}
  <p>You are not logged in</p>
  <!--Github-->
  <a href="{% provider_login_url 'github' %}" ><p class="git">Github</p></a>
  <a href="{% url 'account_login' %}">Login</a>
  <a href="{% url 'account_signup' %}">Sign Up</a>
{% endif %}
{% endblock content %}

views.py:

class HomePageView(generic.TemplateView):
    template_name = "home.html"

urls.py in users app:

from django.urls import path, include
from .views import SignupPageView
from .views import HomePageView

app_name = "users"

    urlpatterns = [
        path("", HomePageView.as_view(), name="home"),
        path("signup/", SignupPageView.as_view(), name="signup"),
    ]

urls.py in project:

from django.contrib import admin
from django.urls import path, include
from users.views import HomePageView


urlpatterns = [
    path("", include("users.urls")),
    path("collab/", include("collab_app.urls"),),  # , "collab_app")),
]

urls.py in collap_app:

from django.contrib import admin
from django.urls import path, include
from collab_app import views
from users.views import HomePageView

app_name = "collab_app"

urlpatterns = [
    path("", views.QuestionListView.as_view(), name="question-list"),
]

view.py in collab_app:

class QuestionListView(ListView):
    model = Question
    template_name = "collab_app/question_list.html"

Upvotes: 3

Views: 1438

Answers (2)

Gorkhali Khadka
Gorkhali Khadka

Reputation: 835

Try changing.

Your urls.py in collap_app:

urlpatterns = [
    path("question-list", views.QuestionListView.as_view(), name="question-list"),
]

In home.html

<a href="{% url 'collab_app:question-list' %}">Ask Question Here</a>

Upvotes: 0

Pedram
Pedram

Reputation: 3920

Your named the url as question-list and you're referencing question_list; it's just a typo.

Use {% url 'collab_app:question-list' %} instead.

Upvotes: 1

Related Questions