Not able to post form data to action url - html

I have a login form. After pressing the login button the the post data is sent to the view login_auth that authenticates the user data and redirects accordingly. However,after pressing the login button, I am not being redirected to the appropriate page.
views.py
def login_successful(request):
return render(request,"login_successful.html")
def login_invalid(request):
return render(request,"login_invalid.html")
def login(request):
return render(request,'login.html',c)
def loginauth(request):
username=request.POST.get("username",'')
password=request.POST.get("password",'')
user=auth.authenticate(username=username,password=password)
if user is not none:
user.login(request.user)
return redirect(login_successful)
else:
return redirect(login_invalid)
urls.py
urlpatterns = [
url(r'^registration/',views.registration),
url(r'^registration_successful/',views.registration_successful),
url(r'^home/',views.home),
url(r'^login/',views.login),
url(r'^login_successful/',views.login_successful),
url(r'^login_invalid/',views.login_invalid),
url(r'^login/auth',views.loginauth)
]
login.html
<html>
<form action="/login/auth" method="POST">{% csrf_token %}
Username :<input type="textbox" name="username" >
Password :<input type="password" name="password">
<input type="submit" value="Login">
</form>
</html>

Your login url pattern is missing a trailing $. It should be:
url(r'^login/$', views.login),
Without the dollar, the /login/auth is matched by r'^login/, so the request is handled by your login view.
It's a bit unusual to process the form on a different url. Django comes with authentication views, including a login view. I would recommend using this rather than writing your own.

Use name for url
views.py
def login_successful(request):
return render(request,"login_successful.html")
def login_invalid(request):
return render(request,"login_invalid.html")
def login(request):
return render(request,'login.html',c)
def loginauth(request):
username=request.POST.get("username",'')
password=request.POST.get("password",'')
user=auth.authenticate(username=username,password=password)
if user is not none:
user.login(request.user)
return redirect('login_successful')
else:
return redirect('login_invalid')
urls.py
urlpatterns = [
url(r'^registration/',views.registration),
url(r'^registration_successful/',views.registration_successful),
url(r'^home/',views.home),
url(r'^login/$',views.login),
url(r'^login_successful/',views.login_successful, name='login_successful'),
url(r'^login_invalid/',views.login_invalid, name='login_invalid'),
url(r'^login/auth',views.loginauth)
]

Related

Struggling to point my form to a view with a variable in Django

Error: NoReverseMatch at /
Reverse for 'language' with keyword arguments '{'name': ''}' not found. 1 pattern(s) tried: ['(?P[^/]+)/\Z']
I am creating a Encyclopedia app in Django which has a form as a input, which will display the search result from my view after searching those input variable in util.py file.
I have created a form like below in my template
<form method="get" formaction="{% url 'language' name=form.name%}">
<input class="search" type="text" name="q" placeholder="Search Encyclopedia
</form>
Here goes my urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("<str:name>/", views.language, name="language"),
]
And the language function in views.py(not writing whole views.py as it will take a lot of space here):
def language(request, name):
if util.get_entry(name):
return render(request, "encyclopedia/entries.html",{
"entries": util.get_entry(name),
"title": name.capitalize()
})
else:
return HttpResponseNotFound("<div style='text-align:center;font- family:sans-serif'><h1>Error</h1><h2> Requested page was not found.</h2></div>")
form.name is empty. You have "entries" and "title" in the rendering context but not form.name

Using django Authentication form with the html form

I am trying to build a login module in django. I already have a nice HTML form for login which has username and password field.
But in my views.py I have imported default Django AuthenticationForm, but if integrate it with my nice-looking HTML form, there will be two forms. How can I import the Authentication form and use it with the my HTML form??
My code is here:
My view:
def login(request):
if request.method == 'POST':
form = AuthenticationForm(data=request.POST)
if form.is_valid():
user = form.get_user()
authlog(request, user)
return redirect('home')
else:
messages.error(request, 'Invalid username or password')
# back_page = request.META.get('HTTP_REFERER')
return redirect('login')
# return HttpResponse(back_page)
else:
content = {
'form': AuthenticationForm()
}
return render(request, 'sign-in.html', content)
If I use Django default form, the code will be following, but it won't look good as my other Html login page.
My sign-in html:
<hr>
<form action="" method="post">
{% csrf_token %}
{{form|crispy}}
<button type="submit" class=" btn btn-success ">Sign-Up</button>
</form>
I haven't posted my other HTML form though as it is very large. Hope it explains.

full_clean() missing 1 required positional argument: 'self'

I'm currently using django version 2.2.4 and trying to create an edit button that will update my models. When trying to save the updated value an TypeError occured which it stated that "full_clean() missing 1 required positional argument: 'self'". I can't seem to detect any error from my codes. Thanks in advance for helping me.
my views.py file
def lab_edit(request, pk, template_name='webapp/lab_edit.html'):
lab= get_object_or_404(Labs, pk=pk)
form = LabForm(request.POST or None, instance=Labs)
if request.method == "POST":
if form.is_valid():
form.save()
return redirect('lab')
return render(request, template_name, {'form':form})
my lab_edit.html file
<div class='container'>
<h2>EDIT LAB</h2>
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
</div>
my LabForm
class LabForm(forms.ModelForm):
class Meta:
model = Labs
fields = ('labcode', 'name','administrator')
Your LabForm gets as instance= the model class, not a model object. You should fix that by passing lab instead:
def lab_edit(request, pk, template_name='webapp/lab_edit.html'):
lab = get_object_or_404(Labs, pk=pk)
if request.method == 'POST':
form = LabForm(request.POST, instance=lab)
if form.is_valid():
form.save()
return redirect('lab')
else:
form = LabForm(instance=lab)
return render(request, template_name, {'form':form})
By passing a reference to the class , you have basically called full_clean on the class, hence the error.
Note that you should not use request.POST or None since an empty POST request can still be a valid POST request.

Invalid form when uploading file in Django

I need to upload file on a Django page, however, after following the official tutorial, I was not able to upload it, it always gives the error "invalid form", and when I tried to print out the error msg of the form, it says "This field is required".
One thing notable is: I have 2 forms on one page, one is this upload form and the other one is for filling out information. Not sure if this is the root cause.
I have tried all solutions provided on the Internet.
Template file:
<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="file" value="upload" name="sourcefile">
<button type="submit">Upload</button>
</form>
Forms.py:
from django import forms
from .models import SourceFile
class UploadFileForm(forms.ModelForm):
class Meta:
model = SourceFile
fields = ('file', 'title')
Models.py:
from django.db import models
# Create your models here.
class SourceFile(models.Model):
title = models.CharField(max_length=255, blank=True)
file = models.FileField(upload_to="media/")
Views.py
def model_form_upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
instance = SourceFile(file_field=request.FILES['file'])
instance.save()
return JsonResponse({'error': False, 'message': 'Uploaded Successfully!'})
else:
print("Invalid form")
# return JsonResponse({'error': True, 'errors': form.errors})
else:
form = UploadFileForm()
return render(request, 'source_validation.html', {'form': form})
Your template is wrong. Either use {{ form.as_p }} which should display a file input field because file is a field in your form. (so remove the <input type="file" ...>)
Or don't use it and manually add the <input> fields, but then you must use the correct names. Your form expects a "file" parameter, not a "sourcefile" parameter:
<input type="file" name="file">
Also, you're overcomplicating things in your view (even though your current code will work if you fix your template):
if form.is_valid():
form.save() # this will save your model
return redirect(...)

Django and the POST request: unexpected behaviour with a form

urls.py
from django.conf.urls.defaults import patterns, include, url
import myproject.views
urlpatterns = patterns('', (r'^$', myproject.views.home), (r'^login$', apolla.views.login))
views.py
import django.http
import django.template
import django.shortcuts
def home(request):
return django.http.HttpResponse("Welcome home!")
def login(request):
un = request.POST.get('username')
pa = request.POST.get('password')
di = {'unam': un, 'pass': pa}
if un and pa:
di['act'] = "/"
else:
di['act'] = "/login"
return django.shortcuts.render_to_response('login.html', di,
context_instance=django.template.RequestContext(request))
# Why does this code not send me immediately to "/" with
# username and password filled in?
login.html
<html>
<head>
</head>
<body>
<form name="input" method="post" action="{{ act }}">
{% csrf_token %}
Username:
<input type="text" name="username"><br>
Password:
<input type="password" name="password"><br>
<input id="su" type="submit" value="Submit"><br>
</form>
</body>
</html>
When I run the development server and go to localhost:8000/login and fill in a username and password and push the submit button I am not sent to localhost:8000/ as I expected from my login function in views.py, I just return to localhost:8000/login. But when I fill in any field and submit for the second time I get directed to localhost:8000.
I also used print un and print pa to see if the post caught the data from the username and password fields and it did from the first time, so why am I not being directed to localhost:8000/login from the first submit with both username and password fields filled in?
You can add redirects to your view by:
from django.http import HttpResponseRedirect
def foo_view(request):
# ...
return HttpResponseRedirect('/')