Could you please help me with Django and custom user model? I'm a new in Django world, so I started to learn from newest version 1.7. In my project I'm using django-allauth package and I want to create OneToOne relationship between standard user class from contrib.auth and my custom model "users".
#models.py
import datetime
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user=models.OneToOneField(User)
f_name=models.CharField(null=True, blank=True, max_length='30')
l_name=models.CharField(null=True, blank=True, max_length='30')
birth_date=models.DateField(null=True, blank=True)
company=models.CharField(null=True, blank=True, max_length='30')
rate=models.FloatField(null=True, blank=True, default=0.0)
skills=models.CharField(null=True, blank=True, max_length='255')
bill_rate=models.CharField(null=True, blank=True, max_length='255')
contacts=models.CharField(null=True, blank=True, max_length='255')
portfolio=models.CharField(null=True, blank=True, max_length='127')
avatar = models.ImageField(upload_to='/static/img/%Y/%m/%d', blank=True, null=True)
def __unicode__(self):
return self.user.username
This is forms.py
#forms.py
from django.forms import ModelForm
from users.models import Profile
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields = ('f_name', 'l_name', 'company', )
This is a template:
{% extends "base_.html" %}
{% block main_content%}
<div>
{% if user.is_authenticated %}
<p>Welcome, {{ request.user.id }}!</p>
<form id="profile_form" method="POST" action="/accounts/profile" enctype="multipart/form-data">
{% csrf_token %}
{{ profile_form.as_p }}
<input type="submit" name="submit" value="Save" />
</form>
{% endif %}
</div>
{% endblock%}
And views.py:
from django.http import HttpResponse
from django.template import RequestContext
from django.conf import settings
from django.shortcuts import render, render_to_response
from models import Profile
from forms import ProfileForm
def edit(request):
profile_form = ProfileForm()
user = request.user
if request.method == 'POST':
profile_form = ProfileForm(data=request.POST, instance=user)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return render_to_response("profile.html", RequestContext(request))
return render_to_response('profile_edit.html', RequestContext(request, {'profile_form' : profile_form}))
I built all of this files according by this tutorial:
http://www.tangowithdjango.com/book/chapters/login.html
And finally, all of this works fine, but when I push Save button, POST form doesn't work. I can't see any changes in database. Redirecting works good, actually it seems that all works fine, but I don't have any data in table (I'm using postgres by the way).
I spent a lot to find the answer and tried almost everything in the internet, but I still have this issue. Maybe I can't understand how it works from django documentation, but I tried the same method and still have a problem.
Thank you in advance!
UPDATE:
My urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
url(r'^$', 'laniakea.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('allauth.urls')),
url('accounts/profile', 'laniakea.views.prof', name='prof'),
url(r'^edit', 'users.views.edit', name='edit'),
)
I found a solution! As Daniel said, I changed "action" attribute to transfer POST request to the same form. And as I discovered, I not created a right instance of User parent class. Actually, I don't need to use them.
#views.py
from django.template import RequestContext
from django.shortcuts import render
from models import Profile
from forms import ProfileForm
def edit(request):
user = request.user
if request.method == 'POST':
profile_form = ProfileForm(request.POST)
if profile_form.is_valid():
profile = profile_form.save(commit=False)
profile.user = user
profile.save()
return HttpResponseRedirect('accounts/profile')
else:
profile_form = ProfileForm()
return render(request, 'profile_edit.html', {'profile_form' : profile_form})
and
#profile_edit.html
{% extends "base_.html" %}
{% block main_content%}
<div>
{% if user.is_authenticated %}
<p>Welcome, {{ request.user.id }}!</p>
<form id="profile_form" method="POST" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ profile_form.as_p }}
<input type="submit" name="submit" value="Save" />
</form>
{% endif %}
</div>
{% endblock%}
Thank you very much for help and good luck!
Related
models.py
from django.db import models
# Create your models here.
class Subscriber(models.Model):
"""A subscriber Model"""
email = models.CharField(max_length=255, blank=False, null=False, help_text="Subscriber Email Address", unique=True)
full_name = models.CharField(max_length=100, blank=False, null=False, help_text="First and Last Name")
class Meta:
verbose_name = "Subscriber"
verbose_name_plural = "Subscribers"
forms.py
from django.forms import ModelForm
from .models import Subscriber
class SubscriberForm(ModelForm):
class Meta:
model = Subscriber
fields = ["email", "full_name"]
views.py
from django.shortcuts import render
from .forms import SubscriberForm
from django.http import HttpResponseRedirect
from django.contrib import messages
# Create your views here.
def subscriber(request):
if request.method == "POST":
subscriber_form = SubscriberForm(request.POST or None)
if subscriber_form.is_valid():
subscriber_form.save()
messages.success(request, "")
return HttpResponseRedirect("/")
else:
subscriber_form = SubscriberForm()
context = {
"form_subscriber": subscriber_form
}
return render(request, "subscriber/subscriber_form.html", context)
subscriber_form.html
{% block content %}
<div>
<form method="POST">
{% csrf_token %}
{{ subscriber_form.as_ul }}
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
Only my submit button is publishing, however the form is never showing up for me.
I have followed the django docs exactly and still am not getting any good results.
It should be form_subscriber not subscriber_form so:
{% block content %}
<div>
<form method="POST">
{% csrf_token %}
{{ form_subscriber.as_ul }}
<input type="submit" value="Submit">
</form>
</div>
{% endblock %}
Additionally, I'd recommend you to only use SubscriberForm(request.POST) in views without using None for GET request as it is already being handled in else condition so:
views.py:
def subscriber(request):
if request.method == "POST":
subscriber_form = SubscriberForm(request.POST)
...
I ran into a situation where I had developed a method called edit and passed both the profile and user forms into it when I wanted to modify a profile.
#login_required
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance=request.user,data=request.POST)
profile_form = ProfileEditForm(instance=request.user.profile,data=request.POST,files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=request.user.profile)
return render(request,'users/edit.html',{'user_form':user_form,'profile_form':profile_form})
this is my views.py where i have written the logic
and coming to urls.py
path('edit/',views.edit,name='edit'),
and the edit.html code follows like this
{% extends 'users/base.html' %}
{% block body %}
<h2>Edit profile form</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ user_form.as_p }}
{{ profile_form.as_p }}
<input type="submit"/>
</form>
{% endblock %}
This is my models.py file
from django.db import models
from django.conf import settings
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
photo = models.ImageField(upload_to='users/%Y/%m/%d',blank=True)
def __str__(self):
return self.user.username
The main project urls.py
from django.contrib import admin
from django.urls import path,include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('users/',include('users.urls'))
]
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
the output shows me like this
I am expecting a better solution to resolve this issue
This means that there is no Profile model instance for that user. You may want to use django postsave signals to create a Profile model instance for any user that has been created.
To test this, use the admin page to create a Profile model instance for that user and check if it still generates the error.
I hope this helps.
I am a Django beginner and I started working on my first project. I implemented a model "extendedUser" with a medic_code field, extending User. It appears to be a problem when displaying the medic_code in a template. It doesn't display the actual property of the user, but the default value: "".
Template
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<div class="media">
<img class="rounded-circle account-img" src="{{ user.profile.image.url }}">
<div class="media-body">
<h2 class="account-heading">{{ user.username }}</h2>
<p class="text-secondary">{{ user.email }} </p>
<p class="text-secondary">{{ user.medic_code }}</p> (empty string here)
</div>
</div>
<!-- FORM HERE -->
</div>
{% endblock content %}
models.py:
from django.db import models
from django.contrib.auth.models import User
class extendedUser(User):
medic_code = models.CharField(max_length=20, default='')
users/forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from users.models import extendedUser
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
medic_code = forms.CharField(max_length=20)
class Meta:
model = extendedUser
fields = ['username', 'email', 'medic_code', 'password1', 'password2']
views.py:
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .forms import UserRegisterForm
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
user = form.save()
#user.refresh_from_db()
user.medic_code = form.cleaned_data.get('medic_code')
user.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Your account has been created! You are now able to log in! Your medic code {user.medic_code}') #correct value displayed here
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
#login_required
def profile(request):
user = request.user
return render(request, 'users/profile.html', {'user':user})
Also after I create a user and display the medic_code field in a Django shell for that user, the proper value is displayed. What may be problem?
Thanks!
I am trying to build a simple landing page in Django that will allow users to sign up for an email newsletter. I am using this cookie cutter template - https://github.com/Parbhat/cookiecutter-django-foundation - because it integrates Foundation 6 from the jump.
The challenge is that the form fields are not showing in the template. Any help would be appreciated.
My models.py is:
class Subscribe(models.Model):
email = models.EmailField()
subscription_status = models.BooleanField(default=True)
create_date = models.DateTimeField(auto_now_add = True, auto_now = False)
update_date = models.DateTimeField(auto_now_add = False, auto_now = True)
def __unicode__(self):
return self.email
My forms.py is:
from django import forms
from .models import Subscribe
class SubscribeForm(forms.ModelForm):
class Meta:
model = Subscribe
fields = ('email',)
My views.py is:
from django.shortcuts import render
from subscribers.forms import EmailForm, SubscribeForm
from .models import Subscribe
def home(request):
form = SubscribeForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=False)
#we might need to do something here.
email = form.cleaned_data['email']
new_join_old, created = Subscribe.objects.get_or_create(email=email)
#new_join.save()
context = {"form": form}
template = "pages/home.html"
return render(request, template, context)
And my template is:
{% extends "base.html" %}
{% load foundation_formtags %}
{% block content %}
<section class="hero">
<!-- HERO SECTION -->
<div class="homebox">
<div class="wrap">
<p>Lorem Ipsum</p>
<form class="form" method="post" action=""> {% csrf_token %}
{{ form|as_foundation }}
<input type='submit' value='Subscribe' class='btn' />
</form>
</div>
</div>
</section>
My urls.py is:
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.conf.urls import url
from . import views
from subscribes.views import home
urlpatterns = [
url(r'^$', home, name='home'),
]
Thanks!
I tried to make simple request form. And I need to redirect user to "thank you" page after successful form sent. But after user hit "send" button - nothing happens. Just reload form page without form cleaning also.
Form is on "call" page, redirect needs "confirm" page...
So, task is: user fill the form on page "call" and after hitting "send" button, goes to "confirm" page.
My model:
# -*- coding: utf-8 -*-
from django.db import models
from django.forms import ModelForm
# Create your models here.
class Join(models.Model):
class Meta():
db_table = 'expert_request'
user_expert = models.CharField(max_length=100)
user_name = models.CharField(max_length=100)
user_cost = models.CharField(max_length=100)
user_email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.user_email
my "forms.py":
from django import forms
from userform.models import Join
class JoinForm(forms.ModelForm):
class Meta:
model = Join
This is my "views.py":
from django.shortcuts import render
from django.shortcuts import HttpResponseRedirect, Http404
# Create your views here.
from userform.forms import JoinForm
from userform.models import Join
def userform(request):
form = JoinForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=False)
new_join.save()
HttpResponseRedirect('/confirm/')
context = {"form": form}
template = "userform.html"
return render(request, template, context)
def confirm(request):
return render(request, 'confirm.html')
This is my URL's:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
url(r'^$', 'expert.views.index', name='index'),
url(r'^(\d+)/?$', 'expert.views.index'),
url(r'^call/$', 'userform.views.userform', name='call'),
url(r'^confirm/$', 'userform.views.confirm', name='confirm'),
)
My template "userform.html":
{% load staticfiles %}
<form style="position:relative" method="POST" action="">{% csrf_token %}
{{ form.user_expert }}
<p style="position:absolute; top:50px; left:20px; color:#FF0000;">{{ form.user_expert.errors.as_text }}</p>
{{ form.user_name }}
<p style="position:absolute; top:182px; left:20px; color:#FF0000;">{{ form.user_name.errors.as_text }}</p>
{{ form.user_cost }}
<p style="position:absolute; top:50px; left:380px; color:#FF0000;">{{ form.user_cost.errors.as_text }}</p>
{{ form.user_email }}
<p style="position:absolute; top:182px; left:380px; color:#FF0000;">{{ form.user_email.errors.as_text }}</p>
<button type="submit">Send</button>
<div class="clear"></div>
</form>
Maybe you forgot the return statement in userform view function? Fix its beginning like this:
def userform(request):
form = JoinForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=False)
new_join.save()
return HttpResponseRedirect('/confirm/')