0

I'm practicing in Django and I want to know how requests and view mechanisms work correct in Django.

I started an app called ghcrawler in my django project. I designed like it has to send responses that recevied from localhost/ghcrawler and localhost/ghcrawler/results

So this is the urls.py in ghcrawler/ app folder.

from django.urls import path, include
from .views import main_view, results_view

urlpatterns = [
    path('', main_view.as_view() , name='ghcrawler'),
    path('ghresults', results_view.as_view(), name='getresults')
]

localhost/grcrawler page works well as expected. I just want to wire the requests coming to localhost/ghcrawler/results to getresults() function in results_view class defined in views.py, however it doesn't even write the 'h1' to the console

ghcrawler/views.py:

from django.views.generic import TemplateView
from django.shortcuts import render
from django import forms
from django.http import HttpResponse
from .github_requester import search_user

class main_view(TemplateView):
    template_name = 'ghcrawler.html'

    # Handle the post request received from /ghcrawler/
    def post(self, request, *args, **kwargs):
        if request.method == 'POST':
            user = search_user(request.POST.get("username", ""))
            
            if user == None:
                print("User not found.")

            else: 
                print(user)
            return HttpResponse("OK")

class results_view(TemplateView):
    template_name = 'ghresults.html'

    def getresults(self, request, *args, **kwargs):
        print('h1')

1 Answer 1

1

Rather than localhost/ghcrawler/results you mapped localhost/ghcrawler/ghresults to your view.

the rigth code would be:

urlpatterns = [
    path('', main_view.as_view() , name='ghcrawler'),
    path('results', results_view.as_view(), name='ghresults')
]

the firs argument in pathis the actual path

the secont argumen is the view

the third argument name is optional and used for addressing your view independant of your path

class results_view(TemplateView):
    template_name = 'ghresults.html'

    def get(self, request, *args, **kwargs):
        print('h1')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.