How to follow on a button click in Django? - python

Views.py - I want to be able to go to a users page, and then click and follow them from a button exactly like twitter, i sort of know how to add users as you can see by my add variable in my view but I really have not clue how to actually implement that into a button that allows me to follow the user! I have been stuck on this for a whole day and it may be very obvious so any help is greatly appreciated! I do not think my template is need for this question but if it is let me know!
#login_required
def home(request, username):
context = {}
if username == request.user.username:
return HttpResponseRedirect('/home /user/{0}'.format(request.user.username))
else:
user = User.objects.get(username=username)
user_profile = UserProfile.objects.filter(user=user)
following = user.userprofile.follows.all()
number = user.userprofile.follows.all().count()
tweet = Tweet.objects.filter(userprofile=user_profile).order_by('date')
yum = Tweet.objects.filter(userprofile=user_profile).count()
add = user.userprofile.follows.add(request.user.userprofile)
context['user'] = user
context['profile'] = user_profile
context['follow'] = following
context['number'] = number
context['tweet'] = tweet
context['yum'] = yum
return render (request, 'homer.html', context)
models.py
from django.db import models
from django.contrib.auth.models import User
import os
def get_image_path(instance, filename):
return os.path.join('photos', str(instance.user.id), filename)
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio = models.CharField(max_length=120, blank=True, verbose_name='Biography')
follows = models.ManyToManyField('self', related_name='followers', symmetrical=False, blank=True)
theme = models.ImageField(upload_to=get_image_path, blank=True)
profile_picture = models.ImageField(upload_to=get_image_path, blank=True)
def __str__(self):
return self.bio
class Tweet(models.Model):
userprofile = models.ForeignKey(UserProfile)
tweets = models.TextField(max_length=120)
date = models.DateTimeField()
def __str__(self):
return self.tweets

You could do this on a GET or a POST. Here's the view it on a GET since that's simpler.
from django.http import JsonResponse
def follow_user(request, user_profile_id):
profile_to_follow = get_object_or_404(UserProfile, pk=user_profile_id)
user_profile = request.user.userprofile
data = {}
if profile_to_follow.follows.filter(id=user_profile.id).exists():
data['message'] = "You are already following this user."
else:
profile_to_follow.follows.add(user_profile)
data['message'] = "You are now following {}".format(profile_to_follow)
return JsonResponse(data, safe=False)
Then in your urls.py you'd need to add the following to your urlpatterns.
url(r'^follow/(?<user_profile_id>[\d]+)/$', views.follow_user)
Then you'd need to use some javascript like the following:
$('.follow-button').click(function() {
$.get($(this).data('url'), function(response) {
$('.message-section').text(response.message).show();
});
});
This assumes some html like the following:
<body>
<div class="message-section" style="display:none;"></div>
{% for user_profile in all_user_profiles %}
<button data-url="{% url "example_app.views.follow_user" user_profile_id=user_profile.id %}"
class="follow-button" type="button">Follow</button>
{% endfor %}
</body>

Related

How to check if URL is in Model/DB already on a URL Shortener? Django

i've been learning Django for these past days and i'm trying to develop an url shortener. It's functional and works great but it misses something: Check if the URL already exists so it can return the short URL stored in db. At this moments it's just checks if the short url is unique and does not exist already, so it always create a new and unique short url for the same URL.
I've tried to use queryset's exists() in if ShortenerForm.objects.filter(url = cleaned_info['url']).exists(): but it always gave me an error object has no attribute 'cleaned_data'
How can i do that?
These are my files:
views.py
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from utils.shorty.form import ShortenerForm
from shorty.models import Shortener
# Create your views here.
def home(request):
template = "shorty/pages/index.html"
context = {}
context["form"] = ShortenerForm()
if request.method == "GET":
return render(request, template, context)
elif request.method == "POST":
used_form = ShortenerForm(request.POST)
if used_form.is_valid():
shortened_object = used_form.save()
new_url = request.build_absolute_uri("/") + shortened_object.shortcode
long_url = shortened_object.url
context["new_url"] = new_url
context["long_url"] = long_url
return render(request, template, context)
context["errors"] = used_form.errors
return render(request, "shorty/pages/index.html")
def redirect_url_view(request, shortened_path):
try:
shortener = Shortener.objects.get(shortcode=shortened_path)
shortener.redirectCount += 1
shortener.save()
return HttpResponseRedirect(shortener.url)
except:
raise Http404("Sorry this link does not exist")
form.py
from django import forms
from shorty.models import Shortener
class ShortenerForm(forms.ModelForm):
url = forms.URLField(
widget=forms.URLInput(
attrs={"class": "form-control", "placeholder": "Enter URL"}
)
)
class Meta:
model = Shortener
fields = ("url",)
models.py
from django.db import models
from utils.shorty.factory import create_short_url
# Create your models here.
class Shortener(models.Model):
startDate = models.DateTimeField(auto_now_add=True)
lastSeenDate = models.DateTimeField(auto_now=True)
redirectCount = models.PositiveIntegerField(default=0)
url = models.URLField()
shortcode = models.CharField(max_length=6, unique=True, blank=True)
class Meta:
ordering = ["-startDate"]
def __str__(self):
return f"{self.url} to {self.shortcode}"
def save(self, *args, **kwargs):
if not self.shortcode:
self.shortcode = create_short_url(self)
super().save(*args, **kwargs)
Thanks for your patience and time.

How can i call a list in django template?

Here is my models.py file.
from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from django.utils.text import slugify
import misaka
from django.contrib.auth import get_user_model
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=255, unique=True)
slug = models.SlugField(allow_unicode=True, unique=True)
description = models.TextField(blank=True, default='')
description_html = models.TextField(editable=False, default='',blank=True)
members = models.ManyToManyField(User, through="CategoryMember")
category_pic = models.ImageField(upload_to = 'category_pics', blank=True)
def __str__(self):
return self.name
# WE are saving the model. But before that we are converting
# the name using slugify and description using misaka.
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
self.description_html = misaka.html(self.description)
super().save(*args, **kwargs)
#get_absolute_url is used because it tell the template
# CreateView to go to the page it is directing.
# for this example it is directing to go to single page of a category.
def get_absolute_url(self):
return reverse("categories:single", kwargs={"slug":self.slug})
class Meta:
ordering = ['name']
class CategoryMember(models.Model):
category = models.ForeignKey(Category, related_name = "memberships", on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name="user_categories", on_delete=models.CASCADE)
def __str__(self):
return self.user.username
class Meta:
unique_together= ("category", "user")
This is some part of views.py file
from django.contrib.auth.models import User
from categories.models import CategoryMember, Category
class UserPosts(ListView):
model = Post
template_name = 'posts/user_post_list.html'
def get_queryset(self):
try:
self.post_user = User.objects.prefetch_related("user_of_post_model").get(
username__iexact=self.kwargs.get("username")
)
except User.DoesNotExist:
raise Http404
else:
return self.post_user.user_of_post_model.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
current_user = UserProfileInfo.objects.filter(
user__id__iexact = self.post_user.id
).get()
i=1
user_categories={}
for member in CategoryMember.objects.all():
if member.user == self.post_user:
user_categories.update({i:member.category.name})
i=i+1
else:
print('not found')
print(user_categories)
if current_user.profile_pic:
profile_pic = True
picture = current_user.profile_pic
edited_picture = get_thumbnail(picture, '350x350', quality=99, format='PNG')
else:
profile_pic = False
root = settings.MEDIA_ROOT
import os
root1 = os.path.join(root, 'profile_pics/no-image.png')
picture = root1
edited_picture = get_thumbnail(picture, '350x350', quality=99, format='PNG')
# resizeimage.resize_cover(picture, [200, 100], validate=False)
# rescale_image(picture,width=100,height=100)
current_user1 = current_user
user_info = {
'current_user':current_user,
'user_categories':user_categories,
'picture':edited_picture,
'profile_pic':profile_pic,
'post_user':self.post_user,
}
context['user_info'] = user_info
return context
After rendering the html page i can see that dictionary has values inside. Here is the values printed in terminal.
not found
not found
not found
not found
not found
{1: 'Regression', 2: 'Classification'}
Now, i want to get user_categories in my django template. i have tried in different approaches but can't get user_categories .
this is some part of my django html file.
<h3>Member of Categories</h3>
{# this line shows error #}
{# {{ userinfo[{{user_categories}}] }} #}
{{ userinfo.user_categories.1 }}
{% for categories in userinfo.user_categories %}
{{categories}}
{% endfor %}
<h5 class="techfont">Post written by {{user_info.post_user.username}} : {{post_list.count}} </h5>
This section only renders user_info.post_user.username but not user_info.user_categories.1
see the text below.
Member of categories.
Post written by abc: 1
user_categories is a dictionary, not a list. So try like this:
{% for idx, categories in userinfo.user_categories.items %}
{{idx}}. {{categories}}
{% endfor %}

user profile of other user shows up

The id of admin is 1 when i open the admin user at the admin panel. Likewise the id of michael is 2 but when i click the profile icon instead of showing me the profile of admin i get profile of michael. To get the id i have used user.id of the requested user.
Also the problem is i could not use slug in such model.
restaurant/base.html
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link user-icon" href="{% url 'userprofiles:profile' user.id %}">
<i class="fa fa-user"></i>
</a>
</li>
{% else %}
userprofiles/urls.py
urlpatterns = [
# url(r'^profile/(?P<profile_name>[-\w]+)/(?P<profile_id>\d+)/$', views.profile, name='profile'),
url(
r'^profile/(?P<profile_id>\d+)/$',
views.profile,
name='profile'
),
]
userprofiles/views.py
def profile(request, profile_id):
if profile_id is "0":
userProfile = get_object_or_404(UserProfile, pk=profile_id)
else:
userProfile = get_object_or_404(UserProfile, pk=profile_id)
user_restaurant = userProfile.restaurant.all()
user_order = userProfile.order_history.all()
total_purchase = 0
for ur in user_order:
total_purchase += ur.get_cost()
return render(
request,
'userprofiles/profile.html',
{
'userProfile':userProfile,
'user_restaurant':user_restaurant,
'user_order':user_order,
'total_purchase':total_purchase
}
)
userprofiles/profile.html
{% for user_restaurant in user_restaurant %}
{{user_restaurant.name}}<br/>
{{user_restaurant.address }}
{% endfor %}
userprofiles/models.py
class UserProfile(models.Model):
user = models.OneToOneField(User)
restaurant = models.ManyToManyField(Restaurant)
order_history = models.ManyToManyField(OrderMenu)
# favorites = models.ManyToManyField(Restaurant)
is_owner = models.BooleanField(default=False)
class Meta:
def __str__(self):
return self.user.username
# def get_absolute_url(self):
# return reverse('userprofiles:profile', kwargs={'slug':self.slug, 'id':self.id})
How can i use slug for such model so that in admin panel the slug for that user be automatically saved? Because there is no post method.
But the main problem is i am getting userprofile of another user.
Just add 1 everywhere you use profile_id
def profile(request, profile_id):
if profile_id is "0": # Is profile_id a string or integer?
userProfile = get_object_or_404(UserProfile, pk=(profile_id+1)) # What does this do?
else:
userProfile = get_object_or_404(UserProfile, pk=(profile_id+1))
user_restaurant = userProfile.restaurant.all()
user_order = userProfile.order_history.all()
total_purchase = 0
for ur in user_order:
total_purchase += ur.get_cost()
return render(request, 'userprofiles/profile.html', {'userProfile':userProfile,
'user_restaurant':user_restaurant,
'user_order':user_order,
'total_purchase':total_purchase })
I suspect that somewhere in your code you've got a n-1 problem (i.e. computers start counting at 0 but humans start counting at 1). I haven't found exactly where it is, but this will probably work as a bandage solution in the meantime.
Also, I'm not sure what that if does in your code, it looks like it would never get used if profile_id is an integer.
I used slug instead of id and for using slug i have used pre_save signal where slug value is taken from the username.
def profile(request, profile_slug):
if profile_slug is None:
userprofile = get_object_or_404(UserProfile,slug=profile_slug)
else:
userprofile = get_object_or_404(UserProfile, slug=profile_slug)
user_restaurant = userprofile.restaurant.all()
user_order = userprofile.order_history.all()
total_purchase = userprofile.total_purchase
return render(request, 'userprofiles/profile.html', {'userprofile':userprofile,
'user_restaurant':user_restaurant,
'user_order':user_order,
'total_purchase':total_purchase})
I filled the value of slug in this way.
def create_slug(instance, new_slug=None):
print('instance',instance.user)
slug = slugify(instance.user.username)
if new_slug is not None:
slug = new_slug
qs = UserProfile.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" %(slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug
def pre_save_post_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
from django.db.models.signals import pre_save
pre_save.connect(pre_save_post_receiver, sender=UserProfile)

how to get the login username while using django-registration-redux

I want to collect the login user's data while using Django. But i don't know how to locate the unique login username when i use django-registration-redux
I have tried to do this, here's my views.py:
from django.shortcuts import render
from yigu.models import Gw, user_data
from django.contrib.auth.models import User
def index(request):
Gw_list = Gw.objects.all()
context_dict = {'cyshici': Gw_list}
return render(request, 'yigu/base.html', context_dict)
def search(request):
if 'gw' in request.GET:
words_1 = request.GET['gw']
result_list = Gw.objects.filter(field_1=words_1)
result_list2 = user_data.objects.filter(word=words_1)
result_list3 = user_data.objects.filter(user=User.username)
# For basic search match and display content
if result_list:
context_dict = {'result_list': result_list}
words = Gw.objects.get(field_1=words_1)
words.views = words.views + 1
words.save()
# For words_information Model
if result_list2 & result_list3:
words2 = user_data.objects.get(word=words_1)
words2.click_times = words2.click_times + 1
words2.save()
else:
words2 = user_data(user=User.username, word=words_1, click_times=words.views)
words2.save()
return render(request, 'yigu/search.html', context_dict)
here's my models.py:
class user_data(models.Model):
user = models.CharField(max_length=255)
word = models.CharField(max_length=255)
click_times = models.IntegerField(default=0)
class Meta:
ordering = ('click_times',)
def __unicode__(self):
return self.user
But the all the username is something like this <class 'django.contrib.auth.models.User'>
Could anyone give me some tips?
User models of using Redux:
from django.contrib.auth.models import User
And add an OneToOneField to user_data:
class UserData(models.Model):
user = models.OneToOneField(User)
clicks = models.IntegerField(default = 0)
def __str__(self):
return self.user.username

Auto fill ForeignKey for Django Model

So I'm building a basic Q&A site-- Each topic has a series of questions associated with it, and each question has multiple answers associated with it.
I'm creating the user input for questions and they have to associated with a topic. This is the questions model
#models.py
class Question(models.Model):
movie = models.ForeignKey(Movie, blank=True, null=True)
question_text = models.CharField(max_length = 1000)
question_detail = models.CharField(max_length = 5000, blank = True, null = True)
q_pub_date = models.DateTimeField(auto_now_add = True)
q_author = models.ForeignKey(User)
class QuestionForm(ModelForm):
def save(self, user = None, force_insert = False, force_update = False, commit = True):
q = super(QuestionForm, self).save(commit = False)
q.q_author = user
if commit:
q.save()
return q
class Meta:
model = Question
exclude = ('movie', 'q_author', 'q_pub_date')
This is the URL conf
#urls.py
url(r'^(?P<movie_id>\d+)/add_question/$', 'add_question'),
Now here is the view
#views.py
def add_question(request, movie_id):
if request.method == "POST":
form = QuestionForm(request.POST, request.FILES)
#QuestionForm.movie = Movie.objects.get(pk = movie_id)
if form.is_valid():
form.save(user = request.user)
return HttpResponseRedirect("/home/")
else:
form = QuestionForm()
return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request))
This is the HTML code
#add_question.html
<h1> Add Question: {{ user.username }}</h1>
<form action = "" method = "post">{% csrf_token %}
{{ form.as_p }}
<input type = "submit" value = "Ask" />
<input type = "hidden" name = "next" value = "{{ next|escape }}" />
</form>
In the view, the commented out line is what I added to the view to try and auto save the model. When adding a question, the URL has the ID of the movie it is associated with, and my thought is to take that ID and then plug it into the ForeignKey to identify which movie is associated with the question. However, when I use my code, it changes all of the Questions' movie associations to the current movie instead of just changing that specific question's movie association. Without the code, it doesn't associate a Movie with the Question at all. How do I fix this?
Use this:
#views.py
def add_question(request, movie_id):
if request.method == "POST":
form = QuestionForm(request.POST, request.FILES)
if form.is_valid():
question = form.save(user = request.user)
question.movie = Movie.objects.get(pk = movie_id)
question.save()
return HttpResponseRedirect("/home/")
else:
form = QuestionForm()
return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request)
For question asked in comment
You should avoid using absolute URLs in views or templates. Consider a scenario, where you decide to change home URL from /home/ to /myhome/. You will have to edit it where ever you have used them. It is always better to name the urls (docs):
# URL Conf
url(r'^home/$', 'home_view', name="home_url"),
url(r'^(?P<movie_id>\d+)/add_question/$', 'add_question', name="add_question_url"),
url(r'^home/(?P<movie_id>\d+)/$', 'movie_view', name="movie_url"),
The name argument act as an unique identifier to your actual URLs
Now in you views:
from django.core.urlresolvers import reverse
def some_view(request):
...
return HttpResponseRedirect(reverse('home_url'))
Now what ever change you make to the URL (say /home/ to /myhome/ makes no effect to the view as long as the name argument has the same value in the URL conf.
If you wish to pass parameters (like movie_id in your case)
def some_view(request, movie_id):
...
return HttpResponseRedirect(reverse('movie_url', kwargs={'movie_id':movie_id}))
The same concept should be used in templates to avoid hard-coding URLS in templates. Please read this for more details

Categories