Question: URL dispatcher not working

Question

URL dispatcher not working

Answers 1
Added at 2016-12-29 09:12
Tags
Question

I am creating a simple form in Django that takes inputs as username, email, password and adds them to database. Now when I click on submit button the URL dispatcher is not redirecting as well as updating the database. Here is my code:

loginForm\urls.py ( loginForm as project ):

from django.conf.urls import url, include from django.contrib import admin

app_name = 'authentication'

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^authentication/', include('authentication.urls',namespace="authentication")),
    ]

authentication\urls.py ( authentication as app ):

from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^$',views.SignIn, name="sign_in"),
    url(r'^register/$',views.Register, name="register"),
    ]

sign_in.html :

{% extends 'authentication/base.html' %}

{% block body %}

<div class="container">

    <div class="row">
        <div class="col-sm-offset-2 col-sm-8 col-md-offset-3 col-md-6 col-lg-offset-4 col-lg-4">
            <fieldset>
                <legend> Register </legend>
                <from method="post" action="{% url 'authentication:register' %}" >
                    {% csrf_token %}
                    <div >
                        <input name="username" type="username" placeholder="Username" class="form-control">
                        <input name="email" type="email" placeholder="Email" class="form-control">
                        <input name="password" type="password" placeholder="Password" class="form-control">
                    </div>
                    <br> <button type="submit" class="btn btn-defaul"> Submit </button>
                </from>
            </fieldset>
        </div>
    </div>

</div>

{% endblock %}

views.py:

from django.shortcuts import render
from .models import users

def SignIn(request):
    return render(request,'authentication/sign_in.html')

def Register(request):
    register = users()
    register.username = request.POST['username']
    register.email = request.POST['email']
    register.password = request.POST['password']
    register.save()
    return render(request,'authentication/profile.html',{'username': register.username })

thanks for your kindness :)

Answers to

URL dispatcher not working

nr: #1 dodano: 2016-12-29 09:12

Firestly update your view as :

from django.shortcuts import render,redirect
from .models import users

def SignIn(request):
    return render(request,'authentication/sign_in.html')

def Register(request):
    register = users()
    if request.method == 'POST':
        register.username = request.POST['username']
        register.email = request.POST['email']
        register.password = request.POST['password']
        register.save()
        return redirect('/your_new_url')
    return render(request,'authentication/profile.html',{'username': register.username })

And secondly update your HTML template code to fucntion the form as :

<form method="post" action="{% url 'authentication:register' %}" >
    {% csrf_token %}
    <div >
        <input name="username" type="username" placeholder="Username" class="form-control">
        <input name="email" type="email" placeholder="Email" class="form-control">
        <input name="password" type="password" placeholder="Password" class="form-control">
    </div>
    <br> 
    <button type="submit" class="btn btn-defaul"> Submit </button>
</form>
Source Show
◀ Wstecz