Django register form not showing up on admin users when submitted - django

I want to create a register and login but when I create account in http://127.0.0.1:8000/register/ it doesn't show in admin (http://127.0.0.1:8000/admin/auth/user/)
views.py
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth import authenticate, login
def register(request):
form = UserCreationForm()
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
context = {'form': form}
return render(request, 'user/register.html', context)
def login(request):
context = { }
return render(request, 'user/login.html', context)
urls.py
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
path('register/', views.register, name = "register"),
path('login/', views.login, name="login"),
]
user/register.html
<form method="POST" action=" ">
{% csrf_token %}
{{form.as_p}}
<input type="submit" name="Create User">
</form>
I need help, Thank you!

Related

Django - UserEditView is missing a QuerySet?

trying to create an edit profile for users and i keep getting this error what should i add or change ? is my models right for UserEditView
this is my views.py (all of it edited)
maybe the vendor its not compatitable with User edit view
anything elses needs to be added or should i just change something
all imports are for vendor and UserEditView
from tkinter import Entry
from django.contrib.auth.models import User
from xml.dom.minidom import Entity
from django.contrib.auth import login
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.views import generic
from django.contrib.auth.forms import UserCreationForm , UserChangeForm
from django.utils.text import slugify
from django.shortcuts import render, redirect
from .models import Profile, Vendor
from products.models import Product
from .forms import ProductForm
# Create your views here.
def become_vendor(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
vendor = Vendor.objects.create(name=user.username, created_by=user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'vendor/become_vendor.html', {'form': form})
#login_required
def vendor_admin(request):
context = {
'user':request.user
}
vendor = request.user.vendor
products = vendor.products.all()
return render(request,'vendor/vendor_admin.html',{'vendor': vendor , 'products': products ,'context':context})
#login_required
def add_house(request):
if request.method == 'POST':
form = ProductForm (request.POST, request.FILES)
if form.is_valid():
product = form.save(commit=False)
product.vendor = request.user.vendor
product.slug = slugify(product.عنوان)
product.save()
return redirect('vendor_admin')
else:
form = ProductForm()
return render(request,'vendor/add_house.html',{'form': form})
class UserEditView(generic.UpdateView):
models = User
form_class = UserChangeForm
template_name = 'vendor/edit_profile.html'
seccess_url = reverse_lazy('vendor_admin')
def get_object(self):
return self.request.user
urls.py
from django.urls import path
from .import views
from .views import UserEditView
from django.contrib import admin
from django.contrib.auth import views as auth_views
urlpattern =[
path('signup/', views.become_vendor, name='become_vendor'),
path('profile/', views.vendor_admin, name='vendor_admin'),
path("logout/", auth_views.LogoutView.as_view(), name="logout"),
path('login/', auth_views.LoginView.as_view(template_name='vendor/login.html'), name='login'),
path('edit_profile/', UserEditView.as_view(template_name='vendor/edit_profile.html'), name='edit_profile'),
]
edit_profile.html
(where the error pops up)
{% extends "base.html"%}
{% load static %}
{% block content %}
<title>title</title>
<div class="section pt-9 pb-9">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section-title">
<div class="wrap-title">
<h3 class="title">
<span class="first-word"></span>
</h3>
<br>
<form method="post" >
{% csrf_token %}
<table>
{{ form.as_p }}
</table>
<button class='button'>Update</button>
</form>
</div>
<hr>
{% endblock content %}
I think that you didn't declare your model correctly:
class UserEditView(generic.UpdateView):
# models = UserChangeForm #That has no sense.
model = User #The name of your model (Probably the default one: User).
form_class = UserChangeForm
template_name = 'vendor/edit_profile.html'
success_url = reverse_lazy('vendor_admin')
def get_object(self):
return self.request.user
Other thing. You have declared your template name twice. According to your views.py you can delete the template_name on your urls.py:
path('edit_profile/', UserEditView.as_view(), name='edit_profile'),

Input redirect me on the wrong view

Hello i'm a beginner at django and i got this problem. (i don't know if the code i put is enough)
When i decide to create a new comment, if i click on "create" instead of redirect me to "usercomments" and send the data to the database, it log me out and send me where log out is supposed to send me. i can't find a similar problem, i don't even know what kind of error it is, help
Thanks :)
Templates of "create comment" :
<div class="create-comment">
<h2>Write a comment</h2>
<form class="site-form" action="{% url 'usercomments:create' %}" method="post">
{% csrf_token %}
{{form}}
<input type="submit" value="Create">
</form>
</div>
Urls.py :
from django.conf.urls import url
from . import views
app_name = 'usercomments'
urlpatterns = [
url(r'^$',views.comments_list, name="list"),
url(r'^create/$', views.comments_create, name="create"),
url(r'^(?P<slug>[\w-]+)/$',views.comments_detail, name="detail"),
]
Views of create comment :
#login_required(login_url="/userprofile/login/")
def comments_create(request):
if request.method == 'POST':
form = forms.CreateComment(request.POST)
if form.is_valid():
# save article to db
instance = form.save(commit=False)
instance.author = request.user
instance.save()
return redirect('usercomments:list')
else:
form = forms.CreateComment()
return render(request, 'usercomments/comments_create.html', { 'form': form })
Views of log out :
def logout_view(request):
if request.method == 'POST':
logout(request)
return redirect('/') <--- Where i get redirected when i press "create"
else:
pass
Urls of logout :
from django.conf.urls import url
from . import views
app_name = 'userprofile'
urlpatterns = [
url(r'^signup/$', views.signup_view, name="signup"),
url(r'^login/$', views.login_view, name="login"),
url(r'^logout/$',views.logout_view, name="logout"),
]

NoReverseMatch (Reverse for ' user_login' not found)

I have a django project called log and an application called basic_app. I created a register form but when I want to create a user_login, I get this error:
NoReverseMatch at /basic_app/user_login/
Reverse for ' user_login' not found. ' user_login' is not a valid view function or pattern name.
views.py:
from django.shortcuts import render
from basic_app.forms import UserForm, UserProfileInfoForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
# Create your views here.
def index(request):
return render(request, 'basic_app/index.html')
#login_required
def special(request):
return HttpResponse('You are loged in!')
#login_required
def user_logout(request):
logout(request)
return HttpResponseRedirect(reverse('index'))
def register(request):
registered = False
if request.method == 'POST':
user_form = UserForm(data=request.POST)
profile_form = UserProfileInfoForm(data=request.POST)
if user_form.is_valid() and profile_form.is_valid():
user = user_form.save()
user.set_password(user.password)
user.save()
profile = profile_form.save(commit=False)
profile.user = user
if 'profile_pic' in request.FILES:
profile.profile_pic = request.FILES[
'profile_pic'
]
profile.save()
registered = True
else:
print(user_form.errors, profile_form.errors)
else:
user_form = UserForm()
profile_form = UserProfileInfoForm()
return render(request, 'basic_app/registration.html',
{'user_form':user_form,
'profile_form':profile_form,
'registered':registered})
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username = username, password = password)
if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return HttpResponse('Account Not Active.')
else:
print('Someone tried to login and failed!')
print('username:{} and password:{}'.format(username, password))
return HttpResponse('invalid login details supplied!')
else:
return render(request, 'basic_app/login.html',{})
basic_app/urls.py:
from django.conf.urls import url
from basic_app import views
#TEMPLATE URLS
app_name = 'basic_app'
urlpatterns = [
url(r'^register/$', views.register, name='register'),
url(r'^user_login/$', views.user_login, name='user_login'),
log/urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from basic_app import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/', include('basic_app.urls')),
url(r'^logout/$', views.user_logout, name='logout'),
url(r'special/', views.special, name='special')
login.html:
{% extends 'basic_app/basic.html' %}
{% block body_block %}
<div class="jumbotron">
<h1>Please Login: </h1>
<form action="{% url 'basic_app: user_login' %}" method="post">
{% csrf_token %}
<label for="username">Username:</label>
<input type="text" name="username" placeholder="Enter Username">
<label for="passwrod">Password:</label>
<input type="password" name="password">
<input type="submit" name="" value="Login">
</form>
</div>
{% endblock %}

Django - Url Dispatching - Page Not Found

I am very puzzled as to why the URL dispatcher is try to look for an empty path although a url was specified for it? Does it imply that it is unable to find the specified url and therefore trying to find the default.
This happens when I try to POST and HttpResponseRedirect searches for an empty path instead of following the specified path. Assume that the other.
Using Django version: 2.0
Thanks in advance!
main/urls.py (ROOT_URLCONF)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('app/', include('shorterner.urls'))
]
shorterner/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('request/', views.RequestView.as_view(), name="request"),
path('list/', views.IndexView.as_view(), name="list")
]
shorterner/views.py
from django.shortcuts import render, redirect
from django.http import HttpResponseRedirect
from django.conf import settings
from django.urls import reverse
from django.views import View, generic
from .models import Urls
import requests
import json
from .forms import SubmitUrlForm
class RequestView(View):
form_class = SubmitUrlForm
initial = { 'url': ''}
template_name = "shorterner/request.html"
context_object_name = 'url'
def form_valid(self, form):
return super().form_valid(form)
def get(self, request, *args, **kwargs):
form = self.form_class(initial=self.initial)
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
input_url = form.cleaned_data['url']
short_url = google_url_shorten(input_url)
print(input_url)
print(short_url)
new_url = Urls.create(short_url, input_url)
new_url.save()
return HttpResponseRedirect('/app/request/')
return render(request, self.template_name, {'form': form})
shorterner/templates/shorterner/request.html
{% extends 'shorterner/base.html' %}
{% block content %}
<form action="/" method="POST">
{% csrf_token %}
<input type="text" name="url" placeholder="Url..." required/>
<input type="submit" value="Submit"/>
</form>
{% endblock %}
Screenshot of the problem
Error Page
Looks like you're sending form data to this path /. But you've not defined that route in main/urls.py.

Django User Registration register.html returns a blank page

I'm a beginner and i have been stuck on this for two days.Here is what i presently have in my views files.
from django.contrib.auth import logout
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.core.context_processors import csrf
def registration(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/successful/')
# args = {}
# args.update(csrf(request))
# args['form'] = UserCreationForm()
# return render_to_response('registration/register.html', args)
else:
form = UserCreationForm()
return render(request, "registration/register.html", {'form': form,})
def main_page(request):
return render(request, 'index.html')
def logout_page(request):
"""
Log users out and re-direct them to the main page.
"""
logout(request)
return HttpResponseRedirect('/')# Create your views here.
def success(request):
return render(request, 'success.html')
In my urls file
from django.conf.urls import patterns, include, url
from django.contrib import admin
from mysiteII.views import *
admin.autodiscover()
# Examples:
# url(r'^$', 'mysiteII.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
urlpatterns = patterns('',
url(r'^$', main_page),
# Login / logout.
url(r'^login/$', 'django.contrib.auth.views.login'),
url(r'^logout/$', logout_page),
# Web portal.
url(r'^portal/', include('portalapp.urls')),
#Serve static content.
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'static'}),
url(r'^register/$', registration),
url(r'^admin/', include(admin.site.urls)),
url(r'^successful/$', success),
)
register.html
{% extends "base.html" %}
<!-- # {% block title %}Create an account{% endblock %} -->
{% block content %}
<h2>Create an account</h2>
<form action = "/register/" method = "post">{% csrf_token %}
{{form}}
<input type="submit" value="Create Account" />
</form>
{% endblock %}
Looks as if the register.html doesnt render forms.ProbAbaly i missed out on something.Please someone to point the right direction.Thank you, lot of Appreciation.
The first step to debug this is to make sure you are rendering the proper template, usually this can be done with django debug toolbar. In this case the register.html template is indeed being loaded. From there check your blocks. It's possible that you don't have a "content" block in your base.html.