I want to make a registration form like this, where user would type username,email,password,confirm password and check the terms and condition box to register his/herself. But i dont know how to create a model row for check box so that according to its input i would allow or disallow user to register.
models.py
class registeration(models.Model):
username = models.CharField(max_length=100)
email = models.EmailField()
password = models.CharField(max_length=100)
confirm_pasword = models.CharField(max_length=100)
def __str__(self):
return self.username
You can use BooleanField for this:
terms_confirmed = models.BooleanField()
Related
I have a Profile model:
from django.contrib.auth.models import User
class Profile(models.Model):
first_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
user = models.OneToOneField(User, on_delete=models.CASCADE)
birthday = models.DateField(null=True, blank=True)
bio = models.TextField(blank=True, max_length=1000)
location = models.CharField(max_length=100, blank=True)
...
And a search contacts view:
class SearchContactsView(ListView):
model = Profile
template_name = 'users/contact_search.html'
context_object_name = 'qs'
def get_queryset(self):
q1 = self.request.GET.get('contact_name')
q2 = self.request.GET.get('contact_location')
if q1 or q2:
return Profile.objects.filter(Q(first_name__icontains=q1) |
Q(last_name__icontains=q1),
location__icontains=q2)
return Profile.objects.all()
It is working fine but I would like to be able to search for contacts via the user field as well. Does anyone know a way to do that?
EDIT my user's username's are created by them when they sign up to the site and are currently uneditable. They are displayed on the admin page via a dropdown since they are a OneToOneField. I think my issue is that django recognises them only as an IntegerField('pk') but I need to somehow cast them as a string value. Am I right in thinking that, and if so how can this be achieved?
You can add to your template to allow user to input user_username and save that username to q3:
q3 = self.request.GET.get('user_username')
After that you can adjust your If condition accordingly, then change your return to something like:
Profile.objects.filter(Q(first_name__icontains=q1) |
Q(last_name__icontains=q1),
location__icontains=q2,
user__username=q3)
I'm working on a project on how to create Two users : buyers/Sellers for Web using Django as Backend.
I've started the app "users"
I've read the Django Documentation about CustomUserModel
But Honestly don't know where to start from.
Any Suggestions?
In my opinion, a Buyer might be a seller and a seller might be a Buyer.
There are some suggestions:
Create your own application named users (This will help you full control User object in future).
Set your AUTH_USER_MODEL settings to users.User: AUTH_USER_MODEL = 'users.User' As you defined.
Define model Seller - OneToOne -> User: This contains seller's properties, Just create when access via user.seller
Define model Buyer - OneToOne -> User: This contains buyer's properties, just create when access via user.buyer
class User(AbstractUser):
# Your user's properties
# Your user's method
#property
def buyer(self):
try:
return Buyer.objects.get(user=self)
except Buyer.DoesNotExist:
default_value_of_buyer = {}
# Or define default value at model fields
return Buyer.objects.create(user=self, **default_value_of_buyer)
#property
def seller(self):
try:
return Seller.objects.get(user=self)
except Seller.DoesNotExist:
default_value_of_seller = {}
# Or define default value at model fields
return Seller.objects.create(user=self, **default_value_of_seller)
class Buyer(models.Model):
"""
You can add this to admin page to make some actions with Buyer
"""
user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, on_delete=models.CASCADE, related_name='buyer_profile')
# Other properties of Buyer
def __str__(self):
return "%s's Buyer profile" % self.user
class Seller(models.Model):
"""
You can add this to admin page to make some actions with Seller
"""
user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, on_delete=models.CASCADE, related_name='seller_profile')
# Other properties of Seller
def __str__(self):
return "%s's Seller profile" % self.user
It is not that hard buddy look:
you create a model User
class User(models.Model):
.........
types = (('b','buyer'),('s','seller'))
user_type = models.CharField(max_length=7, choices=types)
so every field has this argument called choices, it is a tuple that has tuples in it, every sub tuple is a choice and every choice has two elements the first one is what appears in the back end and the second element is what appears in the user interface, so 'b' is what appears in the back end and 'buyer' is what the user see in the form in the web site. Tell if that didn't work for you
class Account(models.Model):
types = (
('B', 'BUYER' ),('S', 'SELLER')
)
name = models.Charfield(max_length=15)
username = models.CharField(max_length=15)
email = models.EmailField()
password = models.CharField(max_length=16)
user_type = models.CharField(max_length=12, choices=types)
TRY THIS AND THEN GO TO YOUR ADMIN PAGE AND YOU WILL UNDERSTAND EVERYTHING
I'm currently creating a Social platform with Django. Right now, I'm developing homepage and want to show posts matching a userfield.
This is my Post model:
class Discussion(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Account, on_delete=models.CASCADE)
post = models.TextField()
date = models.DateTimeField(default=timezone.now)
This is user account model:
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
classid = models.CharField(max_length=10)
This is view:
#login_required(login_url='login')
def home(request):
if request.method == 'GET':
discussions = Discussion.objects.get(post=Account.classid).order_by('-date')
form = PostDiscussionForm()
return render(request, 'app/home.html', {'discussions':discussions,'form':form})
else:
form = PostDiscussionForm(request.POST)
newdisscussion = form.save(commit=False)
newdisscussion.author = request.user
newdisscussion.save()
return redirect('home')
I want to show only logged users matching their classid (from user account model)
What I gather from here, is that you want to show which users are currently online/logged in? For that you'll need to store additional information when logging like, add a boolean field in the user model indicating whether or not the user has logged in and then filter based on that field.
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True)
username = models.CharField(max_length=30, unique=True)
logged_in = models.BooleanField(default=False)
classid = models.CharField(max_length=10)
Now add additional logic which sets this field to true when the user eventually logs in from your /login route.
Then Filter using:
$ return Account.objects.filter(logged_in==True)
This will return you all users that have currently logged in, if you're not using any serializers, than use value_list for getting the values only rather than the whole user objects.
$ return Account.objects.filter(logged_in==True).values_list('classid', flat=True)
I have a model like this:
class Appointment(models.Model):
user = models.ForeignKey(User)
engineer = models.ForeignKey(Engineer)
first_name = models.CharField(max_length=100)
middle_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100)
age = models.IntegerField()
def __unicode__(self):
return self.first_name
I have created a form using this model and I want to save it. Here I want the 'engineer' field to to be as the primary key is passed in the url.. like
engineer = Engineer.objects.get(pk=pk)
How can I do this. Or I should create a normal form and get its value via get method and assign to the field??
Since this is a CreateView, you should build on the example in the documentation and add it in form_valid:
def form_valid(self, form):
form.instance.engineer = Engineer.objects.get(pk=self.kwargs['pk'])
return super(AppointmentCreate, self).form_valid(form)
I am trying to make relationship between two tables, Registered_Users and User_Connections, so that those who are registered can add others in their connection list(more like add friends). Following are contents from models.py:
class Registered_Users(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(unique=True)
password = models.CharField(max_length=10)
def __unicode__(self):
return self.email
class User_Connections(models.Model):
email = models.EmailField(blank=True)
connection_email = models.EmailField(blank=True)
user_connection = models.ForeignKey(Registered_Users, null=True)
def __unicode__(self):
return self.connection_email
What I am trying to do is:
get a registered user:
ru = Registered_Users.objects.get(id=1)
get this registered user's connections from User_Connection:
uc = User_Connections.objects.filter(user_connection=ru)
Now how can I display user connections' email id from User_Connection and first_name for each user connection from Registered_Users.
Let me know if there's a better way to achieve this.
NOTE: user connections' email id will also be present in Registered_Users because all the email ids must be registered.
Appreciate any help. Thanks.
class RegisteredUser(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(unique=True)
password = models.CharField(max_length=10)
connected_users = models.ManyToManyField('self', blank=True, symmetrical=False)
def __unicode__(self):
return self.email
the api now looks like this:
ru = RegisteredUser.objects.get(id=1)
another_user = RegisteredUser.objects.get(email='name#example.com')
ru.connected_users.add(another_user)
uc = ru.connected_users.all()
for user in uc:
print user.first_name, user.last_name, user.email
The Best way to Implement friendship is to use ManyToManyField in Django:
You can read more about it here: https://docs.djangoproject.com/en/1.6/ref/models/fields/#ref-manytomany
Also, to resolve you confusion about query, with 'uc' variable you can just do:
uc.email for email id
and
uc.user_connection.first_name for firstname .
You can read More about Django Queries and objects here : https://docs.djangoproject.com/en/1.6/topics/db/queries/