自定义用户表单的问题 - Django

时间:2017-07-31 22:05:35

标签: python html django

我正在尝试使用Django创建一个网站,我被困在用户注册表单上。当我点击按钮转到注册表单时,它会重定向我,但由于某种原因,表单不在那里。

我将在下面包含HTML和views.py。

views.py

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.views import generic
from django.views.generic.edit import CreateView
from django.views.generic import View
from .forms import UserForm

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


class UserFormView(View):
    form_class = UserForm
    template_name = 'personal/registration_form.html'

    #display blank form
    def get(self, request):
        form = self.form_class(None)
        return render(request, self.template_name, {'form': form})
    # process form data
    def post(self, request):
        form = self.form_class(request.POST)
        if form.is_valid():

        user = form.save(commit=False)

        #cleaned (normalized) data
        username = form.cleaned_data['username']
        password = form.cleaned_data['password']
        user.set_password(password)
        user.save()

        #returns User objects if credentials are correct
        user = authenticate(username=username, password=password)

        if user is not None:
            if user.is_active:
                login(request, user)
                return redirect('personal:index')

    return render(request, self.template_name, {'form':form})

registration_form.html

{% extends 'personal/header.html' %}

{% block body %}

<div class="container-fluid">

<div class="row">

        <div class="col-sm-12 col-md-7">

        <div class="panel panel-default">

            <div class="panel-body">



                <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">

                    {% csrf_token %}

                    {% include 'personal/form-template.html' %}

                    <div class="form-group"></div>

                        <div class="col-sm-offset-2 col-sm-10">

                        <button type="submit" class="btn btn-success">Submit</button>



                        </div>

                </form>



            </div>



        </div>



    </div>



</div>

</div>

{% endblock %}

form_template.html

{% for field in form %}
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <span class="text-danger small">{{field.errors }}</span>
    </div>
        <label class="control-label col-sm-2">{{ field.label_tag }}</label>
    <div class="col-sm-10">{{ field }}</div>
    </div>
{% endfor %}

我不确定问题出在哪里。请帮助!!!

这里的服务器背景也很奇怪......

System check identified no issues (0 silenced).
July 31, 2017 - 20:13:06
Django version 1.11.2, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/  
Quit the server with CTRL-BREAK.
[31/Jul/2017 20:13:34] "GET / HTTP/1.1" 200 2981
Not Found: /https
[31/Jul/2017 20:13:34] "GET /https HTTP/1.1" 404 2564
[31/Jul/2017 20:13:36] "GET /register/ HTTP/1.1" 200 2809
Not Found: /register/https
[31/Jul/2017 20:13:36] "GET /register/https HTTP/1.1" 404 2591
[31/Jul/2017 20:19:28] "GET /blog/ HTTP/1.1" 200 2954
Not Found: /blog/https
[31/Jul/2017 20:19:28] "GET /blog/https HTTP/1.1" 404 2800
[31/Jul/2017 20:19:35] "GET /blog/1 HTTP/1.1" 200 2906
Not Found: /blog/https
[31/Jul/2017 20:19:35] "GET /blog/https HTTP/1.1" 404 2800
[31/Jul/2017 20:19:54] "GET / HTTP/1.1" 200 2981
Not Found: /https
[31/Jul/2017 20:19:54] "GET /https HTTP/1.1" 404 2564
[31/Jul/2017 20:20:04] "GET /register/ HTTP/1.1" 200 2809
Not Found: /register/https
[31/Jul/2017 20:20:04] "GET /register/https HTTP/1.1" 404 2591
[31/Jul/2017 20:20:50] "GET /blog/ HTTP/1.1" 200 2954
Not Found: /blog/https
[31/Jul/2017 20:20:51] "GET /blog/https HTTP/1.1" 404 2800
[31/Jul/2017 20:21:01] "GET /register/ HTTP/1.1" 200 2809
Not Found: /register/https
[31/Jul/2017 20:21:01] "GET /register/https HTTP/1.1" 404 2591
[31/Jul/2017 20:21:47] "GET / HTTP/1.1" 200 2981
Not Found: /https
[31/Jul/2017 20:21:47] "GET /https HTTP/1.1" 404 2564

基本HTML文件

<强烈> header.html中

<!DOCTYPE html>
<html lang="en">

{% load staticfiles %}
    <link rel="stylesheet" href="https"//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <link href="https://fonts/googleapis.com/css?family=Satisfy' rel="stylesheet' type='text/css'>
    <link rel="stylesheet" type="text/css" href="{% static 'personal/css/style.css' %}"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js:></script>
    <script src="http://maxcdn.bootstrapcdn.com/boostrap/3.3.6/js/bootstrap.min.js"></script>

<head>
    <title>rheto</title>
    <meta charset="utf-8" />
    {% load staticfiles %}
    <link rel="stylesheet" href="{% static 'personal/css/bootstrap.min.css' %}" type = "text/css"/>
    <meta name="viewport" content = "width=device-width, initial-scale=1.0">

    <style type="text/css">
        html,
        body {
          height:100%
        }
    </style>
</head>


<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#topNavBar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="/">rheto</a>
    </div>

    <div class="collapse navbar-collapse" id="topNavBar">
        <ul class="nav navbar-nav">
            <li class="">
            <a href="/blog/">
                <span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span><strong>Debate</strong>
                <span class="glyphicon glyphicon-thumbs-down" aria-hidden="true"></span>
            </a>
        </li>
        </ul>

        <form class="navbar-form navbar-left" role="search" method="get" action="#">
        <div class="form-group">
            <input type="text" class="form-control" name="q" value="">
        </div>
        <button type="submit" class="btn btn-default">Search</button>
        </form>

        <ul class="nav navbar-nav navbar-right"> 
        <li class="">
            <a href="/register/">
                <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>&nbsp; <strong>Sign Up</strong>

            </a>
        </li>
        <li class="">
            <a href="#">
                <span class="glyphicon glyphicon-off" aria-hidden="true"></span>&nbsp; <strong>Logout</strong>

            </a>
        </li>
        </ul>

    </div>


    </div>
</nav>
<body class="body" style="background-color:#F0FFFF">
    <div class="container-fluid" style="min-height:95%; ">


        <div class="row">
          <div class="col-sm-2">
          <br>

          <br>
           <!-- Great, til you resize. -->
            <!--<div class="well bs-sidebar affix" id="sidebar" style="background-color:#fff">-->


            <div class='container-fluid'>
            <br><br>
               {% block content %}
               {% endblock %}   
            </div>
          </div>
        </div> 
    </div>
    <footer>
        <div class="container-fluid" style='margin-left:15px'>
            <p>Created by Morty Suckerburdsen</p>
        </div>
    </footer>   

</body>

</html>

个人/ urls.py

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

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^register/$', views.UserFormView.as_view(), name='register'),
    url(r'^contact/$', views.contact, name='contact'),]

网站/ urls.py

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('personal.urls')),
    url(r'^blog/', include('blog.urls')),
    ]

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

  1. 检查您的应用是否列在settings.INSTALLED_APPS
  2. 检查urls.py中是否有指向此视图的条目。
  3. get方法中设置一个断点以确保您点击它 - import ipdb; ipdb.set_trace()

答案 1 :(得分:0)

问题出在您的registration_form.html模板中。您的action参数为空。您没有将url传递给action参数

您的表单action参数需要url。传递url到您的表单action=''参数,

<form class="form-horizontal" action="{% url 'UserFormView_url_here' %}" method="post" enctype="multipart/form-data">