Django Form tag and action attribute

I am not good at English, but no matter how much I try, there is a problem, so I write a question.
I wrote it using a translator, but I can read English a little bit.

I want login page in Django.
I have 2 app.
"mysite", "myapp"

mysite urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

myapp urls.py

from django.urls import path
from myapp import views
from django.contrib.auth import views as auth_view

app_name = 'myapp'

urlpatterns = [
    path('', views.index),
    path('result', views.result),
    path('reuslt2', views.result2),
]

myapp views.py

from django.shortcuts import render, HttpResponse
from django.shortcuts import get_object_or_404, redirect
from django.utils import timezone

# Create your views here.
def index(request):
    return render(request, 'main_page.html')

def login_success(request):
    #login_info = [request.POST['username'], request.POST['password']]
    if request.method == 'POST':
        return HttpResponse('success')
        #return HttpResponse(login_info[0], login_info[1])
    else:
        return HttpResponse('fail')

def result(request):
    return render(request, 'main_page.html')

def result2(request):
    return render(request, 'temp.html')

The login page I want uses two input tags, and the method uses the POST method to get the ID and password input.

I connect 127.0.0.1:8000/result.
I can see 2 input tags.
click submit button. -> connect 127.0.0.1:8000/result2

If the above method is successful, I think it will be possible to communicate using the POST method between HTML configured in a different way.

If I access 127.0.0.1:8000/result, an error such as Reverse for 'result2' not found. 'result2' is not valid view function or pattern name. occurs.

I’m sorry that the sentence is not correct using a translator.

Creating an action attribute in the form tag as {%url ‘result2’%} will cause an error unconditionally and will not be able to show the page.

>Solution :

In urls.py you have a typo: it should probably be result2 instead of reuslt2. I think if you fix this the reverse lookup should work correctly.

You also need to add the name to the paths of the urlpatterns (in urls.py):

path('result/', views.result, name='result'), 
path('result2/', views.result2, name='result2')

Leave a Reply