简体   繁体   English

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

[英]Problems with custom user form - Django

I am trying to create a website with Django, and am stuck on the user registration form. 我正在尝试使用Django创建一个网站,并停留在用户注册表格上。 When I click the button to go to the registration form, it redirects me fine but for some reason the form isn't there. 当我单击该按钮转到注册表格时,它可以重定向我,但是由于某种原因该表格不存在。

I'll include the HTML and views.py below. 我将在下面包含HTML和views.py。

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 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 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 %}

I'm not sure where the problem is. 我不确定问题出在哪里。 Please Help!!! 请帮忙!!!

Here is the server in the background also acting weird... 这是在后台运行的服务器也很奇怪...

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

The base HTML file 基本HTML文件

header.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>

personal/urls.py 个人/ 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'),]

site/urls.py 网站/ 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')),
    ]

You could try the following: 您可以尝试以下方法:

  1. Check that your app is listed in settings.INSTALLED_APPS . 检查您的应用程序是否在settings.INSTALLED_APPS列出。
  2. Check you have an entry in urls.py that points to this view. 检查urls.py中是否有指向该视图的条目。
  3. Put a breakpoint in the get method to make sure you're hitting it - import ipdb; ipdb.set_trace() get方法中放置一个断点,以确保您达到目标import ipdb; ipdb.set_trace() import ipdb; ipdb.set_trace()

Problem is in your registration_form.html template.your action parameter is empty.you didn't pass any url to your action parameter 问题出在您的registration_form.html模板中。您的action参数为空。您没有将任何url传递给您的action参数

Your form action parameter requires a url .pass url to your form action='' parameter, 您的form action参数需要一个url .pass url到您的form action=''参数,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM