0

It keeps showing index.html file instead of register.html file which is supposed to display register form.

my views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.views.generic.edit import FormView
from django.shortcuts import render
from seany_user.forms import registerform

# Create your views here.

def index(request):
    return render(request, 'index.html')

class registerview(FormView):
    template_name = 'register.html'
    form_class = registerform

forms.py

from django import forms


class registerform(forms.Form):
    email = forms.EmailField(
        error_messages={
            'required': 'enter your goddamn email'
        },
        max_length=64, label='email'
    )
    password = forms.CharField(
        error_messages={
            'required': 'enter your password'
        },
        widget=forms.PasswordInput, label='password'
    )
    re_password = forms.CharField(
        error_messages={
            'required': 'enter your password'
        },
        widget=forms.PasswordInput, label='confirm password'
    )

register.html

{% extends "base.html" %}

{% block contents %}
<div class="row mt-5">
  <div class="col-12 text-center">
    <h1>register</h1>
  </div>
</div>
<div class="row mt-5">
  <div class="col-12">
    {{ error }}
  </div>
</div>
<div class="row mt-5">
  <div class="col-12">
    <form method="POST" action=".">
      {% csrf_token %}
      {% for field in form %}
      <div class="form-group">
        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
        <input type="{{ field.field.widget.input_type }}" class="form-control" id="{{ field.id_for_label }}"
          placeholder="{{ field.label }}" name="{{ field.name }}" />
      </div>
      {% if field.errors %}
      <span style="color: red">{{ field.errors }}</span>
      {% endif %}
      {% endfor %}
      <button type="submit" class="btn btn-primary">로그인</button>
    </form>
  </div>
</div>
{% endblock %}

index.html

{% extends "base.html" %}
{% block contents %}
Hello world!
{% endblock %}

url.py

from django.conf.urls import url
from django.contrib import admin
from seany_user.views import index, registerview
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', index),
    url(r'^register/', registerview.as_view()),
]
4
  • Please, can you paste your urls.py and tell which URL are you trying in the browser? Commented Sep 13, 2019 at 6:13
  • 1
    Show what you have added in your urls.py Commented Sep 13, 2019 at 6:13
  • i updated and /register url. it keeps showing index html Commented Sep 13, 2019 at 9:43
  • if you visit 127.0.0.1:8000 the browser will point to index.html page. if yo visit 127.0.0.1:8000/register the browser will point to register.html .. What exactly would you like to achieve? Commented Sep 13, 2019 at 9:54

1 Answer 1

2

You need to terminate your URLs. Currently your index URL matches any string, including "register". It should be:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', index),
    url(r'^register/$', registerview.as_view()),
]

or, better, use the new path syntax:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index),
    path('register/', registerview.as_view()),
]
Sign up to request clarification or add additional context in comments.

1 Comment

(And/or reorder the urls so the index route is last.)

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.