I have created a Customer model in Django and am not able to save the data to the database table and when I click the signup button it just redirects me back to the home page.
I have attached below the customer model and the signup function.
...please help me resolve the issue.
customer.py
from django.db import models
class Customer(models.Model):
first_name= models.CharField(max_length=50)
last_name= models.CharField(max_length=50)
phone=models.CharField(max_length=15)
email=models.EmailField()
password=models.CharField(max_length=500)
def register(self):
self.save()
views.py
def signup(request):
if request.method == 'GET':
return render(request, 'signup.html')
else:
postData=request.POST
first_name=postData.get('firstname')
last_name=postData.get('lastname')
phone=postData.get('phone')
email=postData.get('email')
password=postData.get('password')
print(first_name,last_name,phone,email,password)
customer=Customer(first_name=first_name,last_name=last_name,phone=phone,email=email,password=password)
customer.register()
return HttpResponse("Signup success")
signup.html
{% extends 'base.html' %}
{% block content %}
<div class="container">
<div class="p-4 m-4">
<div class="col-lg-5 mx-auto border rounded pt-4">
<h3 class="alert alert-light border rounded" >Create An Account</h3>
<form action="/" method="POST">
{% csrf_token %}
<!--firstname-->
<div class="form-group">
<label for="">First Name</label>
<input type="text" name="firstname" id="" class="form-control form-control-sm" placeholder="Mike">
</div>
<!--lastname-->
<div class="form-group">
<label for="">Last Name</label>
<input type="text" name="lastname" id="" class="form-control form-control-sm" placeholder="Ross">
</div>
<!--phone-->
<div class="form-group">
<label for="">Phone No</label>
<input type="text" name="phone" id="" class="form-control form-control-sm" placeholder="9876543210">
</div>
<!--email-->
<div class="form-group">
<label for="">Email</label>
<input type="email" name="email" id="" class="form-control form-control-sm" placeholder="abc#gmail.com">
</div>
<!--password-->
<div class="form-group">
<label for="">Password</label>
<input type="password" name="password" id="" class="form-control form-control-sm" placeholder="********">
</div>
<div class="form-group">
<input class="btn btn-sm btn-info" type="submit" value="Sign Up">
</div>
</form>
</div>
</div>
</div>
{% endblock %}
Try this change
Model
def register(self):
return self.save()
Related
I'm running my code through validator and it's coming back with four errors which I don't know how to fix as the code has been imported from django forms.
Does anyone know how to fix these?
<div class="form-group">
<form method="POST">
<input type="hidden" name="csrfmiddlewaretoken" value="...">
<p><label for="id_username">Username:</label> <input type="text" name="username" maxlength="150" autofocus class="form-control" required id="id_username"> <span class="helptext">Required. 150 characters or
fewer. Letters, digits and #/./+/-/_ only.</span></p>
<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" class="form-control" maxlength="100" required id="id_first_name"></p>
<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" class="form-control" maxlength="100" required id="id_last_name"></p>
<p><label for="id_email">Email:</label> <input type="email" name="email" required id="id_email"></p>
<p><label for="id_password1">Password:</label> <input type="password" name="password1" autocomplete="new-password" class="form-control" required id="id_password1"> <span class="helptext">
<ul>
<li>Your password can’t be too similar to your other personal information.</li>
<li>Your password must contain at least 8 characters.</li>
<li>Your password can’t be a commonly used password.</li>
<li>Your password can’t be entirely numeric.</li>
</ul>
</span></p>
<p><label for="id_password2">Password confirmation:</label> <input type="password" name="password2" autocomplete="new-password" class="form-control" required id="id_password2"> <span class="helptext">Enter the same password as before, for verification.</span></p>
<div class="d-grid">
<button class="btn btn-dark ">Register</button>
</div>
</form>
</div>
I have used:
from django import forms
and in my views.py file I used:
from .forms import SignUpForm
my register.html page is as following:
{% extends 'base.html' %}
{% block title %}Register{% endblock %}
{% block content %}
<h1 class="my-4">Register</h1>
<div class="form-group">
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<div class="d-grid">
<button class="btn btn-dark ">Register</button>
</div>
</form>
</div>
{% endblock %}
I am working on a project for a Django web-based application. In this project, there is a section in which I take info from the user through an HTML form. I added a section "image upload " but it gives a MultiValueDictKeyError error when the user does not upload any file. I tried this but not working for me.
This is error section : error image
This is my addpost.html section. It consists of a form through which, I am taking info.
<form action="{% url 'addpost' %}" method='POST' enctype="multipart/form-data" novalidate>
{% include 'includes/messages.html' %}
{% csrf_token %}
{% if user.is_authenticated %}
<input type="hidden" name="user_id" value="{{user.id}}">
{% else %}
<input type="hidden" name="user_id" value="0">
{% endif %}
<div class="row ">
<div class="tex-center">
<div class="row">
<div class="col-md-6 text-left">
<div class="form-group name">
<input type="text" name="author" class="form-control" placeholder="Author"
{% if user.is_authenticated %} value="{{user.first_name}}" {% endif %} readonly>
</div>
</div>
<div class="col-md-6">
<div class="form-group name">
<input type="text" name="title" class="form-control" placeholder="title" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group name">
<input type="text" name="location" class="form-control" placeholder="location" required>
</div>
</div>
<div class="col-md-6">
<div class="form-group name">
<input type="text" name="short_desc" class="form-control"
placeholder="Write short description" required>
</div>
</div>
<div class="col-md-12">
<div class="form-group message">
<textarea class="form-control" name="full_desc"
placeholder="Write full description"></textarea>
</div>
</div>
<input type="file" id="myFile" name="place_photo" required/>
<div class="col-md-12">
<div class="send-btn text-center">
<input type="submit" class="btn btn-outline-success mr-1" value="Send Post">
Cancel
</div>
</div>
</div>
</div>
</div>
</form>
This is the views.py file where I receive POST data
def add_post(request):
if request.method == "POST":
try:
is_private = request.POST['is_private']
except MultiValueDictKeyError:
is_private = False
author = request.POST['author']
title = request.POST['title']
user_id = request.POST.get('user_id')
location = request.POST['location']
short_desc = request.POST['short_desc']
full_desc = request.POST['full_desc']
place_photo = request.FILES['place_photo']
post = Post(author=author, user_id=user_id, title=title, location=location,
short_desc=short_desc,full_desc=full_desc, place_photo=place_photo)
post.save()
messages.success(request,"Your post uploaded successfully")
return render(request,'community/addpost.html')
This is my models.py file
class Post(models.Model):
author = models.CharField(max_length=100, default=' ')
title = models.CharField(max_length=150)
user_id = models.IntegerField(blank=True)
location = models.CharField(max_length=100, default=' ')
short_desc = models.CharField(max_length=255, default='In less than 250 words')
full_desc = models.TextField()
place_photo = models.ImageField(upload_to='photos/%Y/%m/%d/')
added_date = models.DateTimeField(default=datetime.now,blank=True)
image of web page
hello I'm making school project for assignment.
but I don't know how to change the size of background grey box to make adjust with inside E-mail, password... etc box.
here's my code which I'm struggling.
{{!-- 회원 가입 페이지 view --}}
<br/><br/>
<form action="" style-"width: 700px; margin: auto"
<div class="jumbotron">
<div class="col-md-5 col-md-offset-5">
<h1>Sign up</h1>
{{# if hasErrors}}
<div class="alert alert-danger">
{{# each messages}}
<p>{{ this }}</p>
{{/each}}
</div>
{{/if}}
<form action="/user/signup" method="post" style="width: 500px; margin:auto">
<br/>
<div class="form-group">
<label for="email">E-Mail</label>
<input type="text" id="email" name="email" class="form-control">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" class="form-control">
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" class="form-control">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" id="address" name="address" class="form-control">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" id="phone" name="phone" class="form-control">
</div>
<input type="hidden" name="_csrf" value="{{ csrfToken }}">
<button type="submit" class="btn btn-primary pull-right">Sign up</button>
</form>
</div>
</div>
I think it is because you have put everything inside
<div class="jumbotron">
Try moving <div class="jumbotron"> inside <div class="col-md-5 col-md-offset-5">
You might have to change the col-md-5 to col-md-6
I have a form with the following setup:
<div class="row">
<div class="col-3">
#include('acp.sidebar')
</div>
<div class="col-9">
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('acp/provider/add') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="id">
<fieldset>
<legend>Grunddaten</legend>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="description">Beschreibung</label>
<textarea class="form-control" id="description" name="description" required></textarea>
</div>
<div class="form-group">
<label for="url">Domain</label>
<input type="text" class="form-control" id="url" name="url" required>
</div>
<div class="form-group">
<label for="ref">Ref-Code</label>
<input type="text" class="form-control" id="ref" name="ref" required>
</div>
<div class="form-group">
<label for="image">Logo</label>
<input type="file" name="image" id="image" class="form-control" accept="image/*">
</div>
<div class="form-group">
<label for="checkboxes">Payment-Anbieter</label>
<div>
#foreach($paymentProviders as $key => $provider)
<label class="checkbox-inline" for="checkboxes-{{$key}}">
<input name="paymentProviders[]" id="checkboxes-{{$key}}" value="{{$provider->id}}" type="checkbox">
<img src="{{url('/images/paymentProvider/small/'.$provider->image)}}">
{{$provider->name}}
</label>
#endforeach
</div>
</div>
</fieldset>
<fieldset>
<legend>Crawler</legend>
<div class="form-group">
<label for="attributes">Suchattribute</label>
<input type="text" class="form-control" id="attributes" name="attributes" required>
</div>
<div class="form-group">
<label for="crawlerType">Typ</label>
<select id="crawlerType" name="crawlerType" class="form-control">
#foreach($crawlerTypes as $crawlerType)
<option value="{{$crawlerType}}">{{$crawlerType}}</option>
#endforeach
</select>
</div>
</fieldset>
<div class="form-group">
<div class="pull-right">
<button type="submit" class="btn btn-primary">
Absenden
</button>
</div>
</div>
</form>
</div>
</div>
As you can see there is a input named "attributes" down below. When I add this, I can't submit my form. It's like clicking on a button with no "submit" attribute.
After removing or renaming this input, it works. It's not a big thing to rename my input - I'm just interested if this is a bug, or a for me unkown feature.
I'm using Bootstrap v4.0.0-alpha.6 and Laravel 5.4.22.
I've this contact us model and would like to separate it into 2 sections:
name, email, contact number
subject, message
How do I separate these 2 sections into different pages with a BACK and NEXT buttons?
models.py
class ContactUs(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField(max_length=120)
contact_number = models.CharField(max_length=100, null=True, blank=True)
subject = models.CharField(max_length=120, null=True, blank=True)
message = models.TextField(max_length=850)
def __unicode__(self):
return self.name
html
<div class="row">
<div class="col-lg-8 col-lg-offset-2 text-center">
<form method="POST" action='{% url "contact" %}' role="form" id="contact-form" class="contact-form"> {% csrf_token %}
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name" placeholder="Name*" value="{{ name }}">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="E-mail*" value="{{ email }}">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="contact_number" id="contact_number" placeholder="Contact Number" value="{{ contact_number }}">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" value="{{ subject }}">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea class="form-control textarea" rows="3" name="message" id="message" placeholder="Message*">{{ message }}</textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary2 btn-xl page-scroll pull-right">Submit</button>
</div>
</div>
</form>
</div>
</div>
forms.py
class ContactUsForm(forms.ModelForm):
class Meta:
model = ContactUs
fields = ["name",
"email",
"contact_number",
"subject",
"message",]
Up to Django 1.7, you would use the form tools that were shipped with Django.
They were moved out of Django with release 1.8 though. You can find them in a separate module now: https://github.com/django/django-formtools
Among other stuff, it features a so-called form wizard which does exactly what you're looking for.
The documentation is pretty thorough, there is not much I could add without just copy-pasting the code example from there. The creating templates for the forms section has an example with a back and a previous button in it.