我想将表单数据存储到数据库中

时间:2019-09-04 16:57:30

标签: python django python-3.x django-models django-forms

我想将表单数据存储到数据库中(“ subject_code”是表名或模型名),但是当我按下“提交”按钮时,它会抛出错误

错误是

TypeError at /subjectnamewithcode/
_wrapped_view() missing 1 required positional argument: 'request'

请帮助我。

这是我的观点。py

from django.conf.urls import url, include
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import User
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
from django.contrib.auth.mixins import LoginRequiredMixin
# Create your views here.
from .models import signupform, Universityname, facultyhours, subject_code, class_number




def home(request):
    count=User.objects.count()
    return render(request, 'home.html', {
        'count':count
    })

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            usernamedb=request.POST['username']
            passworddb=request.POST['password1']
            emaildb=request.POST['email']
            signup_info=signupform(usernamedb=usernamedb,passworddb=passworddb,emaildb=emaildb)
            signup_info.save()
            form.save()
            return redirect('home')
    else:
        form = UserCreationForm()
    return render(request, 'registration/signup.html', {
        'form': form
    })


@login_required()
def secret_page(request):
    return render(request, 'secret_page.html')



class SecretPage(LoginRequiredMixin,TemplateView):
    template_name = 'secret_page.html'

@login_required()
def University_names(request):
    return render(request, 'createtimetable/university_or_collegename.html')


@login_required()
def universityNames_store_in_database(request):
    university_name=request.POST["universityname"]
    Department_id=request.POST["departmentid"]
    Department_name=request.POST["departmentname"]

    university_dept_info=Universityname(university_name=university_name,Department_id=Department_id,Department_name=Department_name)
    university_dept_info.save()
    return redirect('/faculty_hours/')


@login_required()
def faculty_hours(request):
    return render(request, 'createtimetable/faculty_hours.html',)

@login_required()
def map_faculty_with_hours(request):
    faculty_name=request.POST["facultyname"]
    faculty_unique_name=request.POST["facultyuniquname"]
    faculty_total_hours_in_a_week=request.POST["facultytotalnumberofhours"]

    map_faculty_with_hours_db=facultyhours(faculty_name = faculty_name, faculty_unique_name = faculty_unique_name, faculty_total_hours_in_a_week = faculty_total_hours_in_a_week)
    map_faculty_with_hours_db.save()
    return render(request, 'createtimetable/faculty_hours.html')



@login_required()
def subject_code(request):
    return render(request, 'createtimetable/subjects_code.html')

def subjectnamewithcode(request):
    subject_name=request.POST["subjectname"]
    subject_id=request.POST["subjectcode"]
    subjectnamewithcodedb=subject_code(subject_name = subject_name, subject_id = subject_id)
    subjectnamewithcodedb.save()
    return render(request, 'createtimetable/subjects_code.html')


@login_required()
def class_number(request):
    return render(request, 'createtimetable/class_number.html')


@login_required()
def class_with_numbers(request):
    class_name=request.POST["classname"]
    class_number1=request.POST["classroomnumber"]
    class_with_number_db=class_number(class_name=class_name,class_number=class_number1)
    class_with_number_db.save()
    return render(request, 'createtimetable/class_number.html')

@login_required()
def userdetails(request):
#   allusers=signupform.objects.all()
    return render(request, "secret_page.html")#, {'uusers': allusers})

这是我的模型。py

from django.db import models

# Create your models here.
class signupform(models.Model):
    usernamedb=models.CharField(max_length=250)
    passworddb=models.CharField(max_length=250)
    emaildb=models.CharField(max_length=250)

class Universityname(models.Model):
    university_name=models.CharField(max_length=250)
    Department_name=models.CharField(max_length=250)
    Department_id=models.CharField(max_length=250)


class facultyhours(models.Model):
    faculty_name=models.CharField(max_length=250)
    faculty_unique_name=models.CharField(max_length=250)
    faculty_total_hours_in_a_week=models.CharField(max_length=250)

class subject_code(models.Model):
    subject_name=models.CharField(max_length=250)
    subject_id=models.CharField(max_length=250)

class class_number(models.Model):
    class_name=models.CharField(max_length=250)
    class_number=models.CharField(max_length=250)

这是我的urls.py

from django.contrib import admin
from django.urls import path, include
from mysite.core import views
urlpatterns = [
    path('', views.home, name='home'),
    path('signup/', views.signup, name='signup'),
    path('secret/',views.secret_page, name='secret' ),
    path('secret2/', views.SecretPage.as_view(), name='secret2'),
    path('accounts/', include('django.contrib.auth.urls')),
    path('University_names/', views.University_names, name='University_names'),
    path('universityNames_store_in_database/', views.universityNames_store_in_database, name='universityNames_store_in_database'),
    path('faculty_hours/', views.faculty_hours, name='faculty_hours'),
    path('map_faculty_with_hours/', views.map_faculty_with_hours, name='map_faculty_with_hours'),
    path('subject_code/', views.subject_code, name='subject_code'),
    path('subjectnamewithcode/', views.subjectnamewithcode, name='subjectnamewithcode'),
    path('class_number/', views.class_number, name='class_number'),
    path('class_with_numbers/', views.class_with_numbers, name='class_with_numbers'),
    path('admin/', admin.site.urls),
]

这是我的html表单

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}

<div class="row justify-content-center">
    <div class="col-6">
        <div class="card">
            <div class="card-body">
                <h2>Mapping Subject And Code</h2>

                    <form method="post" action="/subjectnamewithcode/">
                        {% csrf_token %}
                        {{ form|crispy }}
                            <label>Add Subject</label><br>
                            <input type="text" name="subjectname" required class="textinput textInput form-control"><br>
                            <label>Subject Code</label><br>
                            <input type="text" name="subjectcode" required class="textinput textInput form-control"><br>
                            <button type="submit" class="btn btn-primary">ADD</button>
                            <a href="/class_number/" class="btn btn-secondary">NEXT</a>
                    </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

我得到的这个错误

TypeError at /subjectnamewithcode/
_wrapped_view() missing 1 required positional argument: 'request'
Request Method: POST
Request URL:    http://127.0.0.1:8000/subjectnamewithcode/
Django Version: 2.2.4
Exception Type: TypeError
Exception Value:    
_wrapped_view() missing 1 required positional argument: 'request'
Exception Location: /Users/vajra/Documents/DJANGO/mysite/core/views.py in subjectnamewithcode, line 88
Python Executable:  /Users/vajra/Documents/DJANGO/venv/bin/python
Python Version: 3.7.3
Python Path:    
['/Users/vajra/Documents/DJANGO',
 '//miniconda3/lib/python37.zip',
 '//miniconda3/lib/python3.7',
 '//miniconda3/lib/python3.7/lib-dynload',
 '/Users/vajra/Documents/DJANGO/venv/lib/python3.7/site-packages']
Server time:    Wed, 4 Sep 2019 17:40:43 +0000


Traceback Switch to copy-and-paste view

/Users/vajra/Documents/DJANGO/venv/lib/python3.7/site-packages/django/core/handlers/exception.py in inner
            response = get_response(request) …
▶ Local vars
/Users/vajra/Documents/DJANGO/venv/lib/python3.7/site-packages/django/core/handlers/base.py in _get_response
                response = self.process_exception_by_middleware(e, request) …
▶ Local vars
/Users/vajra/Documents/DJANGO/venv/lib/python3.7/site-packages/django/core/handlers/base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
/Users/vajra/Documents/DJANGO/mysite/core/views.py in subjectnamewithcode
    subjectnamewithcodedb=subject_code(subject_name = subject_name, subject_id = subject_id) …
▶ Local vars

0 个答案:

没有答案