I'm trying to make django form to appear after user is logged in and authenticated but the form just does not appear on the page here is my code. I'm stuck on this for few days now and any help would be huge help
Views.py form code
def prasymas(request):
context = initialize_context(request)
user = context['user']
form = Prasymas(request.POST or None)
if form.is_valid():
form.save()
context ={
'form':form
}
return HttpResponseRedirect(reverse('home'))
urls.py code
from django.urls import path
from . import views
urlpatterns = [
# /
path('', views.home, name='home'),
# TEMPORARY
path('signin', views.sign_in, name='signin'),
path('signout', views.sign_out, name='signout'),
path('callback', views.callback, name='callback'),
path('prasymas', views.home, name='prasymas'),
]
template.py code
{% extends "loginas/layout.html" %}
{% block content %}
<div class="container">
<h1 class="d-flex justify-content-center"></h1>
<p class="d-flex justify-content-center"></p>
{% if user.is_authenticated %}
<h4>Sveiki {{ user.firstname }} {{user.surname}}
</h4>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-secondary">POST</button>
</form>
{% else %}<div class="d-flex justify-content-center">
Prisijungti
</div>
{% endif %}
</div>
{% endblock %}
From your urls.py it looks like the prasymas function in views.py is not being called.
Try making the following change to your urls.py file to include a call to the prasymas function.
Note: the line path('prasymas', views.prasymas, name='prasymas'), now points to views.prasymas not home. This means the path /prasymas will now render your form.
from django.urls import path
from . import views
urlpatterns = [
# /
path('', views.home, name='home'),
# TEMPORARY
path('signin', views.sign_in, name='signin'),
path('signout', views.sign_out, name='signout'),
path('callback', views.callback, name='callback'),
path('prasymas', views.prasymas, name='prasymas'),
]
Related
I'm trying to pass an object's id through the url, but its not able to find the page even after it tries the path when it goes through its patterns
Error:
Using the URLconf defined in GroomingService.urls, Django tried these URL patterns, in this order:
admin/
[name='Home']
appointment-Maker/
account/
admin-home
view_appointment/<id>/
login/ [name='Login Page']
registration/ [name='Registration Page']
logout [name='Logout Page']
The current path, adminview_appointment/21/, didn’t match any of these.
GroomingService.urls
#urls
urlpatterns = [
path('admin/', admin.site.urls),
path('', Home_view, name="Home"),
path('appointment-Maker/', include('appointmentApp.urls')),
path('account/', include('accountApp.urls')),
path('admin-home', include('adminApp.urls')),
path('view_appointment/<id>/', viewAppointment_view), #this is the page it is not finding
path('login/', Login_view, name='Login Page'),
path('registration/', Registration_view, name='Registration Page'),
path('logout', Logout_view, name='Logout Page')
]
adminApp/views.py viewAppointment_view
def viewAppointment_view(request, id):
appointments = Appointment.objects.get(id = id)
context = {
'appointments' : appointments
}
return render(request, "admin_templates/viewappointment.html", context)
templates/admin_templates viewappointment.html
{% extends 'base.html' %}
{% block content %}
<a>appointment view</a>
{% endblock %}
templates/admin_templates adminhome.html (the link is clicked through this page)
{% extends 'base.html' %}
{% block content %}
<a>this is the admin page</a>
<br>
{% for a in appointments %}
Client name:{{a.client_dog_name}}<br> {% comment %} this is the link that is clicked {% endcomment %}
{% endfor %}
<br>
<a>find month</a>
<form method="POST">
{% csrf_token %}
{{ monthyear }}
<button class="btn btn-primary" type="submit">click</buttom>
</form>
{% endblock %}
If I'm missing anything please let me know, I had the path at the adminApp/urls.py earlier
urlpatterns = [
path('', views.adminhome_view, name="Admin Home"),
]
but moved it to where it was trying to find the urls. I have no idea why this might not be working.
It should be view_appointment/{{a.id}}, not adminview_appointment/{{a.id}}, but it is better to make use of the {% url … %} template tag [Django-doc]. You can give the view a name:
path('view_appointment/<int:id>/', viewAppointment_view, name='view_appointment'),
and then refer to it in the template:
Client name:{{a.client_dog_name}}<br>
I want to create a tempfriends during creating Moneybook.
So i write a code in a create moneybook template but error occur :
NoReverseMatch at /moneybooks/create/
Reverse for 'create' not found. 'create' is not a valid view function or pattern name.
I write a proper appname, write a urls.py, use single quote...ete but don't know how to achieve it.
moneybook_form.html
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.companion.label}}
</div>
<div class="w-3/4 flex inline border-b my-2 py-3">
<div class="w-3/4">
{{form.companion}}
</div>
<div class= "w-1/4 flex justify-center ">
<i class="fas fa-plus-circle"></i> #this part error occured
</div>
</div>
</div>
{% if form.companion.errors %}
{% for error in form.companion.errors %}
><span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
config/urls.py
urlpatterns = [
path("", include("cores.urls", namespace="cores")),
path('admin/', admin.site.urls),
path("users/", include("users.urls", namespace="users")),
path("tempfriends/", include("tempfriends.urls", namespace="tempfriends")),
path("moneybooks/", include("moneybooks.urls", namespace="moneybooks")),
path("moneylogs/", include("moneylogs.urls", namespace="moneylogs"))
]
moneybooks/urls.py
app_name = "moneybooks"
urlpatterns = [
path("create/", views.moneybook_create.as_view(), name="create"),
path("update/<int:pk>/",
views.moneybook_update.as_view(), name="update"),
path("<int:pk>/", views.moneybook_detail, name="detail")
]
tempfriends/urls.py
from django.urls import path
from . import views
app_name = "tempfriends"
urlpatterns = [
path("create/", views.tempfriend_create.as_view(), name="create"),
]
tempfriends/views.py
class tempfriend_create(FormView):
form_class = forms.CreateTempfriendForm
template_name = "tempfriends/create.html"
def get_current_user(request):
current_user = request.user
def form_valid(self, form):
tempfriend = form.save()
belongs_to = current_user
form.instance.belongs_to = belongs_to
tempfriend.save()
return redirect(reverse("cores:home"))
I just checked my urls.py and I am including my url files without the namespace parameter and it works.
When I use the in-built PasswordResetConfirmView the fields i.e. new password and password confirmation do not show up. I get a blank form with only the submit button.
One of the previous posts mentioned that one could try removing crispy from the corresponding template, which I tried without success.
I also note that the password reset link thatI received in email looks like this: http://127.0.0.1:8000/password-reset-confirm/MQ55a-1dead984b940e457bcad. Note that it is not .../MQ/ as I see in the examples. I tried inserting the / in the url but that did not load the template.
My url patterns...
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name='register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'user/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'user/logout.html'), name='logout'),
path('password-reset/', auth_views.PasswordResetView.as_view(
template_name = 'user/password_reset.html'),
name='password_reset'),
path('password-reset-done/', auth_views.PasswordResetDoneView.as_view(
template_name = 'user/password_reset_done.html'),
name='password_reset_done'),
path('password-reset-confirm/<uidb64><token>', auth_views.PasswordResetConfirmView.as_view(
template_name = 'user/password_reset_confirm.html'),
name='password_reset_confirm'),
path('', include('blog.urls')),
]
My password-reset-confirm.html is
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% crispy form %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Enter New Password</legend>
{{form|crispy}}
</fieldset>
<div class="form-group">
<button type="submit" class="btn btn-secondary">Change Password</button>
</div>
</form>
</div>
{% endblock content %}
I expect to see the password and password confirm fields. Instead I get
the screen which is at this link.
https://drive.google.com/open?id=1E61fjoy5K26nA0Ooej56gRu0DPLFzfXo
I found the problem in my url_patterns:
I had missed out the / between uidb64 and token parameters for the path password-reset-confirm
Following is my urls.py file:
from django.urls import path, re_path, include
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.post_list, name='list'),
path('<slug>/', views.post_detail, name='detail'),
]
My views.py file:
from django.shortcuts import render
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.objects.all().order_by('date')
return render(request, 'blog/post_list.html', {'posts': posts})
def post_detail(request, slug):
post_det = Post.objects.get(slug=slug)
return render(request, 'blog/post_detail.html', {'singlepost': post_det})
And my post_detail.html page:
{% extends 'base.html' %}
{% block content %}
<div class="container-fluid">
<div class="row">
{% for post in posts %}
<div class="post col-md-10 shadow mx-auto">
<!-- post-thumbnail -->
<div class="post-thumbnail">
<img src="{{singlepost.thumb.url}}">
</div>
<!-- /post-thumbnail -->
<h2 class="post-title bg-dark text-light pad1 text-center">{{ singlepost.title }}</h2>
<p class="post-content">{{ singlepost.body }}</p>
<p class="post-info grey border pad1">{{ singlepost.date }}</p>
</div>
{% endfor %}
</div>
</div>
{% endblock content %}
Screenshot from the error page:
Django - Page Not Found Error
What appears to be the problem here? Keep in mind that I'm new to django, however, this is the first time I come across an url such as the one noted in the error report.
I have made few changes,
In urls.py,
path('detail/<slug:slug>/', views.#restofyoururl ),
You have not added your homepage html, so check if you have added the url correctly. In your homepage html
<a href={% url 'blog:detail' post.slug %}> {{post.title}}#add as per your model </a>
Note : You can add class based view for your detail page.
I'm trying to make a simple app that uses the django built-in User model. I have created a registration page, but when I run the server, I get this error at the index page. Here's the code I'm using:
Registration.html
<!DOCTYPE html>
{% extends "basic/base.html" %}
{% block title_block %}
<title>Registration</title>
{% endblock title_block %}
{% block body_block %}
<div class="jumbotron">
{% if registered %}
<h1>Thank you for registering</h1>
{% else %}
<h1>Register here!</h1>
<h3>Fill out the form: </h3>
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{userForm.as_p}}
{{profileForm.as_p}}
<input type="submit" value="Register" name="">
</form>
{% endif %}
</div>
{% endblock body_block %}
Views.py for the 'register' method
def register(request):
registered = False
if(request.method == 'POST'):
userForm = forms.UserForm(data=request.POST)
profileForm = forms.UserProfileInfoForm(data=request.POST)
if((userForm.is_valid()) and (profileForm.id_valid())):
user = userForm.save()
user.set_password(user.password)
user.save()
profile = profileForm.save(commit=False)
profile.user = user
if('profileImage' in request.FILES):
profile.profileImage = request.FILES['profileImage']
profile.save()
registered = True
else:
print(userForm.errors, profileForm.errors)
else:
userForm = forms.UserForm()
profileForm = forms.UserProfileInfoForm()
return render(request, 'basic/registration.html', {'userForm':userForm, 'profileForm':profileForm, 'registered':registered})
This is the urls.py for the project
from django.contrib import admin
from django.urls import path, include
from basic import views
urlpatterns = [
path('', views.index, name='index'),
path('admin/', admin.site.urls),
path('basic/', include('basic.urls', namespace='basic'))
]
This is the urls.py for the basic app
from django.urls import path
from . import views
app_name = 'basic'
urlpatterns = [
path('register/', views.register)
]
And the link to the page in base.html
<a class="nav-link" href="{% url 'basic:register' %}">Register</a>
What can cause the error here?
You must include a name argument to the register route.
path('register/', views.register, name='register')
https://docs.djangoproject.com/en/2.1/topics/http/urls/#reverse-resolution-of-urls
try this (inside your html file)
<a class="nav-link" href={% url 'basic:register' %}>Register</a>
and your urls.py (inside your app) as this:
urlpatterns = [
path('register/', views.register,name='register'),
]
this method worked for me I was fasing the same issue.