How can upload word or pdf file in django - python

I am trying to upload word/pdf file but it's not working. My form submit successfully but file not store in document field. It show empty field and it also not show any error during submit the form, i don't know where is the issue and how can i fix the issue. I add the MEDIA URL in setting and also define the path in urls
View.py
class SaloonRegistration(TemplateView):
template_name = 'saloonRegistration.html'
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
def post(self, request):
saloon = SaloonRegister(
saloon_name=self.request.POST.get('saloon_name'),
owner_name=self.request.POST.get('owner_name'),
address=self.request.POST.get('address'),
contact_no=self.request.POST.get('contact_no'),
document=self.request.POST.get('document')
)
saloon.save()
return redirect('menu')
Model.py
class SaloonRegister(models.Model):
saloon_name = models.CharField(max_length=50)
owner_name = models.CharField(max_length=30)
address = models.CharField(max_length=30)
contact_no = models.BigIntegerField()
document = models.FileField(upload_to='doc/')
is_active = models.BooleanField(default=False)
Template
{% extends 'home.html' %}
{% block content %}
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<label for="saloon_name">Saloon Name
<input type="text" name="saloon_name" placeholder="Enter Your first name">
</label>
<label for="owner_name">Owner Name
<input type="text" name="owner_name" placeholder="Enter Your last_name">
</label>
<label for="address">Address
<input type="text" name="address" placeholder="Enter email address">
</label>
<label for="contact_no">Contact No
<input type="number" name="contact_no" placeholder="Enter your username">
</label>
<label for="document"> upload Doc
<input type="file" name="document" id="document">
</label>
<button type="submit">Submit</button>
</form>
{% endblock%}

Check file upload in the documentation

Related

How to display single/particular data record from database -django

I want to display a particular data record from the database of a person who is logged in to the website. I have a default primary key in the migration. Other than that I don't have any of the primary or foreign keys. Below is the models.py and template codes.
models.py
class Ticket(models.Model):
Email = models.CharField(max_length=40)
to = models.CharField(max_length=40)
cc = models.CharField(max_length=50)
subject = models.CharField(max_length=25)
class Meta:
db_table = 'ticket'
def __str__(self):
return self.Email
Following is the views which I had used before, that display's the whole data from the database.
Views.py
def ticket(request):
ticket = Ticket.objects.all()
return render(request, 'web/ticket.html', {'ticket':ticket})
But I want to display data of the respective mail id
ticket.html
{% for ticket in ticket %}
<fieldset id="first">
<label for="From"><b>From</b></label>
<input type="text" id="From" name="From" style="width:100%" value="{{ ticket.Email }}" required> <br> <br>
<label for="To"><b>To</b></label>
<input type="text" id="To" name="To" style="width:100%" value="{{ ticket.to }}" required><br> <br>
<label for="Cc"><b>Cc</b></label>
<input type="text" id="Cc" name="Cc" style="width:100%" value="{{ ticket.cc }}" required><br> <br>
<label for="Subject"><b>Subject</b></label>
<input type="text" id="Subject" name="Subject" style="width:100%" value="{{ ticket.subject }}" required><br> <br>
</fieldset>
{% endfor %}
Thanking you in advance. You will be highly appreciated if you could explain the needful code.

django models not accept the email?

trying to setting up a small website,a newbie in django . when i create a newsletter setup in django, but the email not saving the admin panel. here is my codes. take a look .
models.py
class Newsletter(models.Model):
email = models.CharField(max_length=200)
def __str__(self):
return self.email
views.py
def Newsletter(request):
if request.method=="POST":
email = request.POST.get("email")
email = email(email=email)
email.save()
message.success(request, "email Successfully added")
return render(request, 'index.html')
urls.py
path('newsletter', views.Newsletter, name="newsletter"),
template
<div id="mc_embed_signup" class="subscribe-form subscribe-form-dec subscribe-mrg">
<form id="Newsletter" class="validate subscribe-form-style" novalidate="" name="Newsletter" method="post" action="/Newsletter">
<div id="mc_embed_signup_scroll" class="mc-form">
<input class="email" type="email" required="" placeholder="Your email address…" name="Newsletter" value="" id="Newsletter">
<div class="mc-news" aria-hidden="true">
<input type="text" value="" tabindex="-1" name="Subscribers">
</div>
<div class="clear">
{% csrf_token %}
<input id="mc-embedded-subscribe" class="button" type="submit" name="subscribe" value="Subscribe">
</div>
</div>
models.py
class Newsletter(models.Model):
email = models.CharField(max_length=200)
def __str__(self):
return self.email
views.py:
Here maybe you are overriding the name of the view with the name of Model, try this:
from .models import Newsletter
def adding_email_to_newsletter(request):
if request.method=="POST":
email = request.POST.get("email")
Newsletter.objects.get_or_create(email=email)
message.success(request, "email Successfully added")
return render(request, 'index.html')
the urls.py
path("adding_email_to_newsletter", views.adding_email_to_newsletter, name="adding_email_to_newsletter"),
template:
Here you miss the csrf_token that is required for post submits by default docs and the using of the tag url for best practices.
<div id="mc_embed_signup" class="subscribe-form subscribe-form-dec subscribe-mrg">
<form id="Newsletter" class="validate subscribe-form-style" novalidate="" name="Newsletter" method="post" action="{% url 'adding_email_to_newsletter' %}">
{% csrf_token %}
<div id="mc_embed_signup_scroll" class="mc-form">
<input class="email" type="email" required="" placeholder="Your email address…" name="Newsletter" value="" id="Newsletter">
<div class="mc-news" aria-hidden="true">
<input type="text" value="" tabindex="-1" name="Subscribers">
</div>
<div class="clear">
{% csrf_token %}
<input id="mc-embedded-subscribe" class="button" type="submit" name="subscribe" value="Subscribe">
</div>
</div>
</form>
</div>

My form gets Submitted but no data is being added in database django

I am trying to make a website's contact page using django where client enters data and it gets submitted in database, the form gets submitted the project runs without errors and yet no data gets added in the db.
Here's my views.py file
from datetime import datetime
from firstapp.models import Contact
# Create your views here.
def index(request):
return render(request,'index.html',)
def apply(request):
return render(request,'apply.html')
def about(request):
return render(request,'about.html')
def socials(request):
return render(request,'social.html')
def contact(request):
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
subject = request.POST.get("subject")
message= request.POST.get("message")
contact=Contact(name=name,email=email,subject=subject,message=message,date=datetime.today())
contact.save
return render(request,'contact.html')
here is my contact.html
{% block title %}Contact {% endblock title %}
{% block body %}
<h2 align="center">CONTACT US </h2>
<div class="container-md">
<form method="POST" action="/contact/">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlInput1">Name</label>
<input type="text" class="form-control" id="exampleFormControlInput1" name="name" placeholder="John Smith">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Email address</label>
<input type="email" class="form-control" id="exampleFormControlInput1" name="email" placeholder="name#example.com">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Subject</label>
<input type="text" class="form-control" id="exampleFormControlInput1" name="subject" placeholder="Business
| Suggestions | Query | Complaint | Other">
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Message</label>
<textarea class="form-control" name="message" id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
{% endblock body%}
And here's my models.py
from django.db import models
# Create your models here.
class Contact(models.Model):
name = models.CharField(max_length=50)
email =models.EmailField(max_length=254)
subject=models.CharField(max_length=10)
message=models.CharField(max_length=1000)
date=models.DateField()
As I wrote at the comments before, you have forgotten to type brackets following the save: contact.save() instead of contact.save.
It would be better and more beautiful if you do it like this:
def contact(request):
if request.method == "POST":
Contact.objects.create(**request.POST)
return render(request,'contact.html')

null value in column "email" violates not-null constraint error in Django using Postgresql

I am trying to save "Contact Us" on my page to a PostgreSQL.
Here is my model:
from django.db import models
from datetime import datetime
# Create your models here.
class Contact(models.Model):
fullname = models.CharField(max_length=200)
email = models.CharField(max_length=50)
message = models.TextField(blank=False)
contact_date = models.DateTimeField(default=datetime.now, blank=False)
user_id = models.IntegerField(blank=True)
def __str__(self):
return self.fullname
Here is my view:
def contact(request):
if request.method == 'POST':
fullname = request.POST['fullname']
email = request.POST.get('email')
message = request.POST.get('message')
contact_date = request.POST.get('contact_date')
user_id = request.POST['user_id']
contact = Contact(fullname=fullname, message=message, email=email, contact_date=contact_date, user_id=user_id)
contact.save()
messages.success(request, 'You have succesfully submitted your message. We will be in touch with you shortly')
return redirect('index')
This is the error I am getting
null value in column "email" violates not-null constraint
DETAIL: Failing row contains (1, Great job guys!, null, null, null, 9).
I typed Great job guys into the drop message field.
This is how the HTML looks like
<form class="containerForm" action="{% url 'contact'%}" method="POST">
{% 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 %}
<input type="text" name="fullname" id="fullname" class="form-control" placeholder="Full Name">
<input type="text" name="fullname" id="email" class="form-control" placeholder="Email Address">
<input type="text" name="fullname" id="message" class="form-control" placeholder="Drop A Message">
<input type="submit" value="Send A Message">
</form>
Kindly help. I have spent some hours on this.
You have the same input names for fullname, email, message.
Try this HTML
<form class="containerForm" action="{% url 'contact'%}" method="POST">
{% 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 %}
<input type="text" name="fullname" id="fullname" class="form-control" placeholder="Full Name">
<input type="text" name="email" id="email" class="form-control" placeholder="Email Address">
<input type="text" name="message" id="message" class="form-control" placeholder="Drop A Message">
<input type="submit" value="Send A Message">
</form>

How to pass data from HTML form to Database in Django

Can you help me with this issue? I want to pass data from HTML form to Database.
This is my HTML form:
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if user.is_authenticated %}
<label for="username">Username: </label>
<input id="username" type="text" name="username" value="{{ user.username }}" disabled><br>
<label for="image">Image: </label>
<input id="image" type="file" name="image">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Save Changes</button>
</div>
</div>
{% endif %}
</form>
And this is my Views.py
class CreateProfile(CreateView):
template_name = 'layout/add_photo.html'
model = Profile
fields = ['image', 'user']
success_url = reverse_lazy('git_project:index')
When I fill in the form (username is equal to logged in username, and chose an image) and when I click "Save Changes", the data has to be passed in Database in table Profile. Here is class Profile in models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.FileField()
I know this is basic, but I don't know how to fix it up, so please, help! :)

Categories