Django form.py not updating - python

I have been going through the Django forms 'tutorial'. Once I had read through the tutorial, I tried modifying it to suit my needs and customize it to learn it Django forms well. I discovered whenever I modified the form, the website would not update. I assume its an error with my code, but I have not been able to find it.
# views.py
def contact(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = ContactForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/message_recived/')
# forms.py
from django import forms
class ContactForm(forms.Form):
name = forms.CharField(label='Name', max_length=100)
email = forms.EmailField(label='Email', max_length=100)
message = forms.CharField(label='Message', max_length=500)
# models.py
from django.db import models
class Contact(models.Model):
name = models.CharField(max_length=100)
email = models.CharField(max_length=100)
message = models.CharField(max_length=500)
and here is the contact.html template:
#contact.html
{% extends "BlogHome/headerAndFooter.html" %}
{% block content %}
<script>
document.title = "Pike Dzurny - Contact"
</script>
<form action="/message_recived/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
{% endblock %}
Did I do something wrong? I have tried clearing my browsers cache, using a new browser, and obviously refreshing it.

Looks like your forget to render response inside your view.
Also you need to include form into context to render template right.
Try to change view as follow:
def contact(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = ContactForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/message_recived/')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})

Related

Why is my Django form not "valid"? Can't get the POST request to update database

I am trying to create a user profiles for users in my Django app. I have the form displaying where I want it to and when I try to submit, nothing happens.
I put a print statement after the form.is_valid in my view.py and found that it wasn't 'valid' but I have no idea why.
I have tried several different ways to 'clean' / 'validate' data but I can't get past the form being 'invalid.'
Any help would be greatly appreciated!
urls:
path('userinfo/', views.user_info, name='userinfo')
form template:
{% extends "base.html" %}
{% load bootstrap4 %}
{% block content %}
<div class="container">
<h1>Enter User Info</h1>
<form method="POST" class="form">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" class="btn btn-primary" value="Create Profile">
</form>
</div>
{% endblock %}
view:
def user_info(request):
form = ProfileForm()
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
form.save()
else:
form = ProfileForm()
return render(request, 'miraDashboard/form.html', context={'form': form})
model:
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
name = models.CharField("Full Name", max_length=1024)
job_role = models.CharField("Role", max_length=254, default="Seeking Job Opportunities")
zip_code = models.CharField("Zip Code", max_length=5)
user_image = models.ImageField("Upload Profile Picture", upload_to='images/')
def __str__(self):
return f'{self.user.username} Profile'
form:
from django.forms import ModelForm
from .models import Profile
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ['name','job_role','zip_code', 'user_image']
if you want to see errors in form change else statmant:
def user_info(request):
form = ProfileForm()
if request.method == 'POST':
form = ProfileForm(request.POST)
if form.is_valid():
form.save()
else:
print(form.errors.as_data()) # here you print errors to terminal
return render(request, 'miraDashboard/form.html', context={'form': form})
after form.is_valid() you don't need to set it again (form = ProfileForm() in else statment). this way your form will get errors( you cen see them in form.errors).

Django view not passing IP address data from form

Trying to do something simple here where I pass an IP address from a form to another view. Previously I had success following beginner tutorials. If someone could point me back to what I'm missing that would be a huge help.
model:
class GenericIP(models.Model):
name = models.CharField(max_length=50)
genericip = models.GenericIPAddressField()
def __str__(self):
return self.name
form:
class IpForm(forms.Form):
gateway = ModelChoiceField(queryset=GenericIP.objects.order_by('name').values_list('genericip', flat=True).distinct())
views:
def TestCreate(request):
if request.method == 'GET':
form1 = IpForm()
return render(request, 'create_test.html', {'form1' : form1} )
else:
if request.method == 'POST':
form1 = IpForm()
if form1.is_valid():
genericip = form1.cleaned_data
genericip.save()
return render(request, 'create_test.html', {'genericip' : genericip} )
def RunTest(request, genericip=""):
if request.method == 'POST':
Server = genericip
return HttpResponse(Server)
URLS:
urlpatterns = [
path('', views.TestCreate, name='create_test'),
path('run_test', views.RunTest, name='run_test',),
]
template:
{% block content %}
<form action="{% url 'run_test' %}"method='post'>
{% csrf_token %}
{{ form1 }}
<input type='submit' class="btn btn-success" value='Run Test'>
</form>
{% endblock %}
So what's happening is when I hit the button to run the test, I don't get anything for the httpresponse. The post data for the TestCreate view does show variable "genericip" and input "192.168.100.100" but that data is not posting correctly to the runtest view.
In the RunTest function, you are getting the genericip value as a function argument.But in your scenario you are sending the genericip vaule in the form of form submit. So you should try some thing like this,
def RunTest(request):
if request.method == 'POST':
ip = request.POST.get('gateway') # Some thing with form field name
Server = ip
return HttpResponse(Server)
Hope this helps you, if anything please let me know.
You aren't adding the POST data to the form instance in the post request - you're just instantiating an empty IpForm instance.
Try something like this in the appropriate section of your code:
if request.method == 'POST':
form1 = IpForm(data=request.POST)

Django form is not valid but no error is sent

I started to learn Django today, but I am stuck at using forms. I have created two forms: /contact and /blog-new. The form at the Contact page is working fine, but the one at /blog-new is redirecting me to the home page after the submission button is pressed and no information is printed in the terminal nor saved in the database.
Code on Github
I appreciate if someone can explain to me what I did wrong as I cannot figure it out. Thank you!
mysite/blog/forms.py
from django import forms
from .models import BlogPost
class BlogPostModelForm(forms.ModelForm):
class Meta:
model = BlogPost
fields = ['title', 'slug', 'content']
mysite/blog/views.py
from .forms import BlogPostModelForm
def blog_post_create_view(request):
# create objects
# ? use a form
# request.user -> return something
form = BlogPostModelForm(request.POST or None)
if form.is_valid():
print(form.cleaned_data)
form.save()
form = BlogPostModelForm()
template_name = 'form.html'
context = {'form': form}
return render(request, template_name, context)
mysite/blog/models.py
from django.db import models
# Create your models here.
class BlogPost(models.Model):
title = models.TextField()
slug = models.SlugField(unique=True)
content = models.TextField(null=True, blank=True)
mysite/mysite/urls.py
from blog.views import (
blog_post_create_view,
)
urlpatterns = [
..
path('blog-new', blog_post_create_view),
..
]
mysite/templates/form.html
{% extends "base.html" %}
{% block content %}
{% if title %}
<h1>{{ title }}</h1>
{% endif %}
<form method='POST' action='.'> {% csrf_token %}
{{ form.as_p }}
<button type='submit'>Send</button>
</form>
{% endblock %}
You need to point to right url in action attribute of form.
<form action="/blog-new/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
I think it's not necessary in your case but you could also refactor your view to match the docs.
from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import SomeForm
def some_view(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = SomeForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = SomeForm()
return render(request, 'template_name.html', {'form': form})
You need to point to right url in action attribute of form.
That was not actually the solution but something that helped me to figure out what was wrong.
It is not necessary to point to /blog-new/ as . for action will point to the same page, but I have tried with /blog-new/ as action URL and I was surprised to see that /blog-new/ page doesn't exist.
The bug was in mysite/mysite/urls.py for missing a /:
path('blog-new', blog_post_create_view),
It is funny (and annoying) how a symbol like / missing from your code will mess up everything and make you spend hours trying to find a solution as simple as that.
Thank you for your time spend to have a look over my code and try to help me!

Django - template build-in filter tags {% url %}

I am a new coder with Django. So, first apologize for it if this question is too easy.
class CommentForm(forms.Form):
comment = forms.CharField(widget=forms.Textarea)
def save_comments_into_database(topic, user_id, content):
data = Comment(topic=topic, commenter_id=user_id, content=content)
data.save()
this is the code for form
<form action = "{% url 'post:comment' the_topic=topic user_id=1 %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">submit</button>
</form>
I am trying to use url tag to call a function is views.py.
topic is a variable I passed in when this page is created.
this is my code in urls.py
url(r'^(?P<topic_id>[0-9]+)/comment/$', views.comment, name="comment"),
then this is how I do in views.py
def comment(request, the_topic, user_id):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = CommentForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
text = form.cleaned_data['comment']
args = {'form': form, 'text': text, 'topic': the_topic}
# save the data in database
save_comments_into_database(the_topic.id, user_id, text)
# redirect to a new URL:
return render(request, 'post/detail.html', args)
# if a GET (or any other method) we'll create a blank form
else:
form = CommentForm()
return render(request, 'post/detail.html', {'form': form, 'topic': the_topic})
I get the NoReserveMatchException:
I really don't get where it goes wrong.
Your comment URL only has one var, the topic_id, but you passed two vars, the_topic and user_id. You need to pass just the topic ID. Also, in views you would normally access the current user via request.user.
you should to change declare the urls, added second parameter and change name of the first
url(r'^(?P<the_topic>[0-9]+)/comment/(?P<user_id>[0-9]+)/$', views.comment, name="comment"),
# ^^^^^^^^^ ^^^^^^
It's better to serve the action redirection using Django built in HttpResponseRedirect in the views.py and combine it with reverse of your url Django docs ref, check bellow and change yourapp_path with your app path and name_space with url name like "blog".
main urls.py with "name_space"
like namepsace="blog"
url(r'^', include('yourapp_path.urls', namespace='name_space))
# ^^^^^^^^^^^^ ^^^^^^^^^^
app urls.py with name="name"
like name="comment"
url(r'^(?P<topic_id>[0-9]+)/comment/$', views.comment, name="name")
# ^^^^^^
reverse with name_space:name
will return the complete path of the url taking name_space:name lets say blog:comment
reverse('name_space:name')
reverse with kwargs or args
reverse('name_space:name', kwargs={'kw1': 'val1'})
reverse('name_space:name', args=['val1'])
HttpResponseRedirect()
302 redirect to a given url in that case we will pass the reverse url.
like: HttpResponseRedirect(reverse('blog:comment', kwargs={'topic_id': topic.id}))
HttpResponseRedirect(reverse('name_space:name', kwargs={'kw1': 'val1'}))
views.py
#import HttpResoneRedircet and reverse
from django.shortcuts import HttpResponseRedirect, reverse
def comment(request, the_topic, user_id):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = CommentForm(request.POST)
# check whether it's valid:
if form.is_valid():
# your is_valid()
return HttpResponseRedirect(reverse('name_space:name', kwargs={'the_topic': the_topic.id}))
template
<form action="{{ action }}" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">submit</button>
</form>

Attaching a current User object to Django form

I am working on an app that has a section with with a file upload form for .txt fiels. I would like for the current user that is uploading the file to be added along with the file and the file name. Currently, I can do this successfully in the admin section but I just cant get it to save via the form itself. Any Ideas?
Here are the models:
class UploadedTextFile(models.Model):
file = models.FileField(upload_to="textfiles")
filename = models.CharField(max_length = 50)
username = models.ForeignKey(User, blank=True, null=True)
class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file', 'filename']
Here is my view:
def inputtest(request):
#response for file being submited
if request.method == "POST":
form = UploadedTextFileForm(request.POST)
if form.is_valid():
new_form = form.save(commit=False)
new_form.username = request.user
new_form.save()
return render(request, 'about.html')
inputtest = UploadedTextFileForm()
return render(request, 'failed.html', {'inputtest': inputtest})
else:
inputtest = UploadedTextFileForm()
return render(request, 'inputtest.html', {'inputtest': inputtest})
Here is my html:
{% extends 'base.html' %}
{% block content %}
<form method="post">{% csrf_token %}
{{ inputtest.as_p }}
<input type="submit" value="Submit" />
</form>
{% endblock content %}
Doing it in the view (as you've shown) is the right way to do this. Most likely you're having problems because you've left username as a field on the form, and because the FK model field doesn't have blank=True set the form requires the field to be provided. You should explicitly declare just the subset fields that you want to accept user input for in the form's Meta class.
class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file', 'filename']
I am not sure why you're rendering a different template when the form is not valid, but no matter what you're not providing the form object in the context. This means that you'll never see any errors the form detects, which is probably what's happening with this code - you're not seeing the error that username is not provided.

Categories

Resources