I have a model which extends the User model of django like this:
class UserProfile(models.Model):
user = models.OneToOneField(User, verbose_name="user details")
rewardpoints = models.IntegerField("rewardpoints")
def __str__(self):
return "%s's profile" % self.user.username
my forms.py
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ['rewardpoints']
class UserForm(ModelForm):
class Meta:
model = User
fields = ['username', 'password']
my view.py
class UserProfileFormView(View):
def post(self, request, *args, **kwargs):
userform = UserForm(request.POST, prefix='users')
userprofileform = UserProfileForm(request.POST, prefix='userprofiles')
if userform.is_valid() and userprofileform.is_valid():
new_user = userform.save() #### Error over here.
new_userprofile = userprofileform.save(commit=False)
new_userprofile.user = new_user
new_userprofile.save()
return HttpResponseRedirect(reverse('users:welcome'))
else:
userform = UserForm(prefix='users')
userprofileform = UserProfileForm(prefix='userprofiles')
return render(request, 'users/signup.html', {'user_form': userform, 'userprofile_form': userprofileform})
def get(self, request, *args, **kwargs):
userform = UserForm(prefix='users')
userprofileform = UserProfileForm(prefix='userprofiles')
return render(request, 'users/signup.html', {'user_form': userform, 'userprofile_form': userprofileform})
my signup.html
<p> Hello Signup!! </p>
<form action="{% url 'users:signup' %}" method="post">
{% csrf_token %}
{{ user_form.username.label_tag }}
{{ user_form.username }}
{{ user_form.password.label_tag }}
{{ user_form.password }}
{{ userprofile_form }}
<input type="submit" value="Submit" />
</form>
</html>
But while making the post request, there is this error: null value in column "rewardpoints" violates not-null constraint
Please show me a way out of this.
The error is because your field rewardpoints needs some value, it can't be null, so, or pass a value to rewardpoint through your form or you can set a default value to it(Recommended). Then when you create a userprofile, form will set 0 as initial value.
class UserProfile(models.Model):
user = models.OneToOneField(User, verbose_name="user details")
rewardpoints = models.IntegerField(default=0)
Related
Is there a way for the button to do the following? : When user press the button it takes the user.username of the current user and automatically fill up a form of BookInstance from models.py and save it to the database.
From models.py :
class BookInstance(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
book = models.ForeignKey("Book", on_delete=models.RESTRICT, null=True)
imprint = models.CharField(max_length=200, blank=True, null=True)
due_back = models.DateField(blank=True, null=True)
borrower = models.ForeignKey(
User, on_delete=models.SET_NULL, blank=True, null=True)
LOAN_STATUS = (
('m', 'Maintenance'),
('o', 'On Loan'),
('a', 'Available'),
('r', 'Reserved')
)
status = models.CharField(
max_length=1, choices=LOAN_STATUS, blank=True, default='a')
class Meta:
ordering = ['due_back']
def __str__(self):
return f'{self.id} - {self.book.title}'
def get_absolute_url(self):
return reverse("catalog:book_list")
class Book(models.Model):
title = models.CharField(max_length=50)
author = models.ForeignKey(
'Author', on_delete=models.SET_NULL, null=True)
summary = models.TextField(
max_length=500, help_text="Enter brief description")
isbn = models.CharField('ISBN', max_length=13, unique=True)
genre = models.ManyToManyField(Genre, help_text="Select genre")
language = models.ForeignKey(
"Language", on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse("catalog:book_detail", kwargs={"pk": self.pk})
This is my from my views.py :
def borrowBook(request, pk):
context = {
'book_instance': BookInstance.objects.all()
}
success_url = reverse_lazy('catalog:index')
if request.method == "POST":
form = BorrowForm(request.POST or None)
if form.is_valid():
book_instance.id = BookInstance.objects.get(pk=pk)
book_instance.book = BookInstance.objects.get(book=book)
book_instance.borrower = request.user
book_instance.status = 'o'
book_borrowed_count = BookInstance.objects.filter(
owner=request.user).count()
if book_borrowed_count < 4:
book_instance = form.save(commit=False)
book_instance.save()
else:
print("Maximum limit reached!")
return redirect('catalog:index')
return render(request, 'catalog/book_detail.html', {'form': form})
here's from my BorrowForm from forms.py :
class BorrowForm(forms.ModelForm):
class Meta:
model = BookInstance
fields = '__all__'
here's my from my urls.py :
path("book_list/book/<int:pk>/borrow", views.borrowBook, name="borrowBook"),
I also tried using a CBV here:
class BorrowBookView(PermissionRequiredMixin, CreateView):
permission_required = 'login'
model = BookInstance
fields = '__all__'
template_name = 'catalog/borrow_form.html'
success_url = reverse_lazy('catalog:index')
def post(self, request, *args, **kwargs):
book_instance.id = BookInstance.objects.get(pk=pk)
book_instance.book = BookInstance.objects.get(book=book)
book_instance.borrower = request.user
book_instance.status = 'o'
book_instance = form.save(commit=False)
book_instance.save()
CBV path from urls.py :
path("book_list/book/<int:pk>/borrow/",
views.BorrowBookView.as_view(), name="book_borrow"),
Here's how I implemented the button using suggestions from here:
<form action="#" method="post">
{% csrf_token %}
<button
type="submit"
class="btn btn-dark flex-shrink-0 "
value="{{ book.id }}">Borrow
</button>
but when I pressed it doesn't seem to save anything to the database and just popup errors, though I may implemented the button or the function from my is views wrong. Thanks and appreciate for any help provided.
You do not need a Django form for this. Forms are usually used for when you want to create objects or edit its fields (like in the admin page). While here an user is not editing nor creating an object (book), but borrowing one.
So basically, we just need to list all available book instances (status='a'), and have a button to "borrow" it. The borrow action is to update status to 'r' or 'o' and have the borrower updated to the current user which is guaranteed to exist inside the request object by LoginRequiredMixin
views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import View
from django.contrib import messages
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from .models import BookInstance
class BorrowBook(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
book_id = kwargs['pk']
available_books = BookInstance.objects.filter(book__pk=book_id, status='a')
return render(request, 'borrow_book.html', {'available_books': available_books})
def post(self, request, *args , **kwargs):
book_instance_id = request.POST['id']
obj = get_object_or_404(BookInstance, id=book_instance_id)
obj.status = 'r'
obj.borrower = request.user
# Maybe also update due_back data
# obj.due_back = ...
obj.save()
messages.success(request, "Your book is reserved.")
# I used the redirection to the same template
# But you probably want to send the user somewhere else
return HttpResponseRedirect(reverse('core:borrow-book', kwargs={'pk': 1}))
borrow_book.html
{% extends 'base.html' %}
{% block content %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% for instance in available_books %}
<form action="{% url 'core:borrow-book' instance.book.id %}" method="POST">
{% csrf_token %}
<input type="hidden" name="id" value="{{instance.id}}">
<p>{{instance.book}}</p>
<p>{{instance.book.language.name}}</p>
<input type="submit" value="Borrow this book.">
</form>
{% endfor %}
{% endblock content %}
urls.py
from django.urls import path
from core import views
app_name = 'core'
urlpatterns = [
path("book_list/book/<int:pk>/borrow/", views.BorrowBook.as_view(), name="borrow-book"),
]
I am trying to implement a dropdownlist from my models into an HTML using Django. The form is working perfectly, but the dropdownlist is not displayed, it only displays "languages:" but not a dropdownlist or any options.
Any ideas?
add_page.html
{% extends 'main/header.html' %}
{% block content %}
<br>
<form method="POST">
{% csrf_token %}
{{form.as_p}}
<button style="background-color:#F4EB16; color:blue"
class="btn btn-outline-info" type="submit">Add Page</button>
</form>
{% endblock %}
forms.py
from django import forms
from django.forms import ModelForm
from main.models import Pages
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.db import models
class NewUserForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def save(self, commit=True):
user = super(NewUserForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
class PagesForm(ModelForm):
class Meta:
model = Pages
fields = ["title","language","content"]
models.py
from django.db import models
from datetime import datetime
#from django.forms import ModelForm
# Create your models here.
LANGUAGE_CHOICES = (
('english','ENGLISH'),
('spanish', 'SPANISH'),
('german','GERMAN'),
('french','FRENCH'),
('chinese','CHINESE'),
)
#Pages son entradas del diario
class Pages(models.Model):
title = models.CharField(max_length=300)
content = models.TextField()
author = models.CharField(max_length=50)
published_date = models.DateTimeField("Published: ", default=datetime.now())
language = models.CharField(max_length=7, choices=LANGUAGE_CHOICES, default='english')
def __str__(self):
return self.title
views.py
#/pages/my_pages/add_page
#login_required
def add_page(request):
if request.method == "POST":
form = PagesForm(request.POST)
if form.is_valid():
model_instance = form.save(commit=False)
model_instance.author = request.user.username
model_instance.timestamp = timezone.now()
messages.success(request, f"New page created, thank you: {model_instance.author}")
model_instance.save()
return redirect('/')
else:
form = PagesForm()
return render(request, "main/add_page.html", {'form': form})
I've got nothing else to say, but the editor wont let me edit unless I write more text...
I have to insert the post through a form but when i submit i got this error FOREIGN KEY constraint failed, the problem is with the author field
models.py
class Post(models.Model):
STATUS_CHOICES = (
('draft','Draft'),
('published','Published'),
)
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=120)
author = models.ForeignKey('auth.User',related_name='blog_posts',on_delete=models.CASCADE,blank=True, null=True)
body = RichTextField()
created = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,choices=STATUS_CHOICES,default='draft')
tag = models.OneToOneField(Tag,related_name="blog_tag",on_delete=models.CASCADE,default=0)
def __str__(self):
return self.title
views.py
def tagView(request,name):
tag = Tag.objects.get(name=name)
post_form = PostForm(request.POST or None)
if request.method == 'POST':
post_form = PostForm(request.POST)
if post_form.is_valid():
item = post_form.save(commit=False)
item.author = request.user
item.save()
return HttpResponseRedirect(request.path_info)
context = {
'post_form' : post_form,
'tag' : tag,
}
return render(request,'blog/tagPage.html',context)
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title','body']
template
<form class="post-form" method="POST" enctype="multipart/form-data" action="">
{% csrf_token %}
{{ post_form }}
<input class="post-form-submit submit" type="submit" value="Save">
</form>
If the author field is the problem, be sure to import auth.User
My guess is that you are trying to add the post as anonymous but anonymous user is not null (id is though). Also don't use 'auth.User' and follow the instructions from the documentation.
I've checked similar posts about forms not showing up at all and I've tried applying the fixes, but it did not fix it for me. I have the following:
stocks.html:
<form method="post" class="form-inline my-2 my-lg-0">
{% csrf_token %}
{{ symbol_form }}
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Add Stock</button>
</form>
views.py:
class PortfolioStockListView(ListView):
model = StockPortfolio
template_name = 'stocks.html'
def post(self, request):
symbol_form = StockSymbolForm(request.POST)
if request.method == 'POST':
if symbol_form.is_valid():
model_instance = symbol_form.save(commit=False)
model_instance.timestamp = timezone.now()
model_instance.save()
return redirect('/')
else:
return render(request, 'stocks.html', {'symbol_form': symbol_form})
else:
symbol_form = StockSymbolForm()
return render(request, 'stocks.html', {'symbol_form': symbol_form})
forms.py:
class StockSymbolForm(ModelForm):
class Meta:
model = StockPortfolio
fields = ['username', 'stock_symbol' , 'stock_qty']
models.py:
class StockPortfolioUser(models.Model):
username = models.OneToOneField(User, on_delete=models.CASCADE)
usercash = models.PositiveIntegerField(default=100000)
class StockPortfolio(models.Model):
username = models.ForeignKey(StockPortfolioUser, on_delete=models.CASCADE)
stock_symbol = models.CharField(max_length=5)
stock_qty = models.PositiveIntegerField(default=0)
How to fix the issue that is causing the forms to hide until the button is clicked? I can share more code from other files if necessary.
Pass it to your template overwriting get_context_data method
class PortfolioStockListView(ListView):
model = StockPortfolio
template_name = 'stocks.html'
def get_context_data(self, *, object_list=None, **kwargs):
context = super(PortfolioStockListView, self).get_context_data(object_list=object_list, **kwargs)
context['symbol_form'] = StockSymbolForm()
return context
def post(self, request):
...
I created a form based on several ModelForm elements. All fields work fine excluding the
ImageField. Due to the missing ImageField form.is_valid() always returns False - even though I pass request.FILES to form.
Why the form with the ImageField is always invalid / empty?
Forms
class UserProfileForm2(forms.ModelForm):
class Meta:
model = models.UserProfile
fields = ['description', 'picture']
class LocationForm(forms.ModelForm):
class Meta:
model = models.Location
fields = ['city', 'state', 'country']
class UserForm(forms.ModelForm):
class Meta:
model = registration_models.User
fields = ['first_name', 'last_name']
Models
class Location(models.Model):
city = models.CharField(max_length=100)
state = models.CharField(max_length=100)
country = models.CharField(max_length=100)
def __unicode__(self):
return ' - '.join([self.city, self.state, self.country])
class UserProfile(models.Model):
authenticationuser = fields.AutoOneToOneField(AuthUser)
description = models.TextField()
picture = models.ImageField(upload_to='uploaded_files/', null=True)
location = models.ForeignKey(Location, null=True)
appear_in_public_ranking = models.BooleanField(default=True)
def __unicode__(self):
return self.authenticationuser.username
View
#login_required
def changeprofile(request):
form = None
# user posted his new profile settings
if request.method == 'POST':
user_form = myforms.UserForm(request.POST)
user_profile_form = myforms.UserProfileForm2(request.POST, request.FILES)
location_form = myforms.LocationForm(request.POST)
forms_are_invalid = not (user_form.is_valid() and user_profile_form.is_valid() and not location_form.is_valid())
if forms_are_invalid:
forms = {'user_form':user_form,
'user_profile_form':user_profile_form,
'location_form':location_form}
return shortcuts.render(request, 'changeprofile.html', forms)
location_form.save()
user_form.save()
user_profile_form.save()
return HttpResponseRedirect('/profile')
else:
forms = {'user_form':user_form,
'user_profile_form':user_profile_form,
'location_form':location_form}
return shortcuts.render(request, 'changeprofile.html', forms)
Template
<form action="{% url 'changeprofile' %}" method="post">
{% csrf_token %}
{{ user_form }}
{{ location_form }}
{{ user_profile_form }}
<input type="submit" value="Submit" />
</form>
If your form contains file input then you must set enctype in your form as:
<form action="{% url 'changeprofile' %}" method="post" enctype="multipart/form-data">
Otherwise request.FILES will always be empty.