So, I am having some difficulty trying to slugify a title field in my model and still have it return the proper information.
Currently, a user can follow the url, if the list in their account exists under this regular expression:
url(r'^user/(?P<username>\w+)/list/(?P<listname>\w+)/$', mylistpage, name='lists'),
The issue I face is that the user can have a list containing spaces, but the regex bases their url off their list name. I am wanting to implement a slug url, but still have it retrieve the correct model/object information.
I am trying to have a slug field and then pre-populate it based on the list name, but I am lost at how this implementation is supposed to work. Much appreciation in advance from any insight.
Model
class newlist(models.Model):
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slugurl = models.SlugField(default = slugurl(self))
def __str__(self):
return self.list_name
def slugurl(self):
return slugify(self.list_name)
Views
def mylistpage(request, username, listname):
context = RequestContext(request)
#make sure that the user is authenticated
if username == request.user.username:
#If the user is authenticated, then perform the following functions to the page
if request.user.is_authenticated():
#Store the current user request object into a variable
user = User.objects.get(username=username)
#Store the list name to the item that starts with the url input
listname = request.user.newlist_set.filter(list_name__iexact=listname)
listitems = request.user.newlist_set.all()
if not listname:
return redirect('/notfound')
else:
return redirect('/notfound')
return render_to_response('listview.html', {'lista': listname}, context)
I have used django-autoslug to great success. You can find a live example here.
SlugField is just a char field with a little syntactic sugar.
You will want to name your slug just slug so django can find it automatically in the URL resolution and passes the right parameter to views.
Your amended code would look like:
from autoslug import AutoSlugField
from django.db import models
class Newlist(models.Model): # Classes start with uppercase names by default
user = models.ForeignKey(User)
list_name = models.CharField(max_length = 100,)
picture = models.ImageField(upload_to='profiles/', default = "/media/profiles/default.jpg")
slug = AutoSlugField(populate_from='list_name')
def __str__(self):
return self.list_name
Your View:
def mylistpage(request,username, slug):
context = RequestContext(request)
#make sure that the user is authenticated
if username == request.user.username:
#If the user is authenticated, then perform the following functions to the page
if request.user.is_authenticated():
#Store the current user request object into a variable
user = User.objects.get(username=username)
#Store the list name to the item that starts with the url input
listname = request.user.newlist_set.filter(slug=slug)
listitems = request.user.newlist_set.all()
if not listname:
return redirect('/notfound')
else:
return redirect('/notfound')
return render_to_response('listview.html', {'lista': listname}, context)
urls.py
url(r'^user/(?P<username>\w+)/list/(?P<slug>[\w-]+)/$', mylistpage, name='lists'),
Related
How to show user's information (special ID, firstName, lastname) in another template after successful form submission in Django.
I have a form to ask users information(general information, Education, experience) and I give random unique test_id. After the user-submitted the form successfully, I have to show his/her test_id to memorize.
I didn't have an idea about view form. My solution is a bit stupid
My model:
class UserForm_uz(models.Model):
test_id = models.CharField(default=random_string,max_length=5,editable=False,unique=True)
rasm = models.ImageField(upload_to='media/rasmlar',null=True,blank=True)
jobName = models.ForeignKey(Job, on_delete=models.CASCADE)
lastName = models.CharField(max_length=200)
firstName = models.CharField(max_length=200)
middleName = models.CharField(max_length=200,blank=True,null=True)
birthData = models.DateField()
nation = models.CharField(max_length=50,blank=True,null=True)
My view:
class FormAfterView(View):
def get(self,request):
obj = UserForm_uz.objects.all().last()
test_id = obj.test_id
firstName = obj.firstName
lastName = obj.lastName
return render(request,"formafter.html",{"test_id":test_id,"firstName":firstName,"lastName":lastName})
You can pass your queryset directly to your Django template and use that queryset to display data accordingly. So your function will like this...
View
class FormAfterView(View):
def get(self,request):
obj = UserForm_uz.objects.all().last()
context = {
'user_details': obj,
}
return render(request,"formafter.html", context)
Django Templates
<p>{{user_details.test_id}}</p>
<p>{{user_details.firstname}}</p>
........
Also, you should try to pass parameters with URLs to fetch data from models.
Using obj = UserForm_uz.objects.all().last() does work but it's not the preferred way and sometimes it can give you wrong data if you placed an ordering query in your models class. What you can do is
URL file
urlpatterns = [
path("form_after_view/<string:main_id>", views.FormAfterView, name="form_after_view"),
]
Form save view
class FormAfterView(View):
def post(self,request):
form = Formclass(request.POST)
if form.is_valid():
form.save()
return redirect('form_after_view', form.main_id)
Next Step View
class FormAfterView(View):
def get(self,request, main_id):
obj = UserForm_uz.objects.get(main_id=main_id)
This will give you the exact instance that you just saved in previous step view.
When i use my super acc, this error does not shows up, but when I tried to use other acc. this error shows up. where did I do wrong?
The error : DoesNotExist at /voting/
Userdata matching query does not exist.
My Model :
class Userdata(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
faculty = models.ForeignKey(Fakultas, on_delete=models.CASCADE, default=1)
is_voted = models.BooleanField(default=False)
def __str__(self): return self.user.username
My views :
#login_required
def voted(response):
user = Userdata.objects.get(id=response.user.id) # get the username
if user.is_voted:
return render(response, 'Main/voting.html', {'calon': Voting.objects.order_by('id'), 'hasil': 'You Have Voted'})
if response.method == 'POST':
id = response.POST['idcalon']
calon2 = Voting.objects.get(id=id) # get user selection in html
user.is_voted = True
calon2.voters += 1
user.save()
calon2.save()
return render(response, 'Main/voting.html', {'calon': Voting.objects.order_by('id')}) # balik ke sendiri
User and UserData don't have the same primary key. So Userdata.objects.get(id=response.user.id) will not work because there is no such Userdata with the given user's id. But you do not need that, because User and Userdata have OneToOne relation. So simply use this:
user = response.user.userdata
I'm fairly new to Django and am working on making user profile pages accessible by using the user's username in the url, e.g. mysite.com/profile/someusername
I'll be having links to the profile in a couple places, but the first place I'm experimenting on is in my navbar to access the logged-in user's page.
base.html
<a class="dropdown-item" href="{% url 'fillups:user_profile' username=user.username %}" class="btn btn-simple">Overview</a>
This correctly displays the currently logged-in user's name, for the case of this example we'll user the username seconduser
This is the url pattern I'm using for this:
path('profile/<str:username>/',views.UserProfile.as_view(),name='user_profile')
So far, the navbar will display the username, seconduser, and when I click the button I'm brought to the url /profile/seconduser/, which is what I want.
The problem is, I'm not able to now use the username in my view to query the objects for the given user. Here is what I have for this view so far
views.py
class UserProfile(TemplateView):
template_name = 'fillups/user_profile.html'
slug_field = "username"
slug_url_kwarg = "username"
def get_context_data(self, **kwargs):
context = super(UserProfile, self).get_context_data(**kwargs)
usr = get_object_or_404(User, username=self.kwargs.get("username"))
overview_stats = {
'total_cars': Car.objects.filter(username=usr).count(),
'total_fillups': Fillup.objects.filter(username=self.request.user).count(),
'total_distance': Fillup.objects.filter(username=self.request.user).aggregate(Sum('trip_distance')),
'total_gallons': Fillup.objects.filter(username=self.request.user).aggregate(total_gallons = Round(Sum('gallons'),4)),
'avg_price': Fillup.objects.filter(username=self.request.user).aggregate(avg_price = Round(Avg('price_per_gallon'),3)),
'total_spent': sum_total_sale(Fillup.objects.filter(username=self.request.user)),
'avg_mpg': avg_mpg(Fillup.objects.filter(username=self.request.user))
}
context['stats'] = overview_stats
context['active_cars'] = Car.objects.filter(status='Active').filter(username=self.request.user)
context['last_10_fillups'] = Fillup.objects.filter(username=self.request.user).order_by('-date')[:10]
return context
For now, everything in the overview_stats dict is what I originally had when I was just querying stuff for the logged-in user, where there was just a simple "myprofile" url. The problem I'm having her is that the get_object_or_404 isn't finding the user. I know that username=self.kwargs.get("username") is getting 'seconduser' like it should be, but for some reason I just can't get the user.
For some extra info, here is one of my models:
class Car(models.Model):
username = models.ForeignKey(User,on_delete=models.CASCADE)
name = models.CharField(max_length=25)
make = models.CharField(max_length=25)
model = models.CharField(max_length=25)
model_year = models.IntegerField(choices=MODEL_YEARS)
status = models.CharField(max_length=10,choices=STATUS,default='Active')
def __str__(self):
return self.name
And in the initial Django tutorial I did, the instructor said it is best to extend the user model so it's easier to make changes, so I have this in a separate app, accounts/models.py
class User(auth.models.User,auth.models.PermissionsMixin):
def __str__(self):
return "#{}".format(self.username)
I've tried using the method in this question which is why I have the slug field stuff in my view currently, and while my question is essentially a duplicate of this question
I've been stuck on this all night and would really appreciate any help, thanks!
Remove the the self from self.kwargs.get("username"). It should be kwargs.get("username").
kwargs is an argument not on object property.
i want to create a follower option in django using DRF so here my models.py
class Connect(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, unique = True, related_name = 'rel_from_set',on_delete=models.CASCADE)
following = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name = 'follwed_by')
def __str__(self):
return str(self.following.all().count())
urls.py
url(r'^conn/(?P<id>\d+)', ConnectApi),
serializer.py
class ConnectSerializer(serializers.ModelSerializer):
class Meta:
model=Connect
fields=('user','following')
views.py
#api_view(['GET','POST'])
def ConnectApi(request,id):
user_id=request.POST.get('id')
print(user_id)
# user_id=request.GET['id']
# action=request.POST.get('action')
if user_id :
# if user_id and action:
try:
user1=User.objects.get(id=user_id)
if user1 :
Connect.objects.get_or_create(user=request.user,
following=user1)
else:
Connect.objects.filter(user=request.user,
following=user).delete()
return JsonResponse({'status':'ok'})
except:
return JsonResponse({'status':'ki'})
return JsonResponse({'status':'ko'})
but whenever i fired up the url
http://127.0.0.1:8000/connect/conn/2
this is the response
{"status": "ko"}
and in the terminal i could see "NONE "
for print(user_id) command
i don't understand where i have done the mistake and i dont think if this is the best practice to make followers function if you know one please let me know
You are passing the id through url and it's directly available inside the view function. So, you don't want to query it from somewhere else
So, use this,
#api_view(['GET', 'POST'])
def ConnectApi(request, id):
user_id = id
print(user_id)
# your code
with django 1.5.1 I try to use the django form for one of my models.
I dont want to add the "user" field (Foreignkey) somewhere in the code instead of letting the user deceide whoes new character it is.
My Code:
Model:
class Character(models.Model):
user = models.ForeignKey(User)
creation = models.DateTimeField(auto_now_add=True, verbose_name='Creation Date')
name = models.CharField(max_length=32)
portrait = models.ForeignKey(Portrait)
faction = models.ForeignKey(Faction)
origin = models.ForeignKey(Origin)
The form:
class CreateCharacterForm(forms.ModelForm):
class Meta:
model = Character
fields = ['name', 'portrait', 'faction', 'origin']
The view:
def create_character(request, user_id):
user = User.objects.get(id=user_id)
if request.POST:
new_char_form = CreateCharacterForm(request.POST)
if new_char_form.is_valid():
new_char_form.save()
return HttpResponseRedirect('%s/characters/' % user_id)
else:
return render_to_response('create.html',
{'user': user, 'create_char':new_char_form},
context_instance=RequestContext(request))
else:
create_char = CreateCharacterForm
return render_to_response('create.html',
{'user': user, 'create_char': create_char},
context_instance=RequestContext(request))
I have tried to use a instance to incluse the userid already. i've tried to save the userid to the form before saving it, or changing the save() from my form.
I keep getting the error that character.user cant be null
I have to tell that im pretty new to django and im sure one way or another it should be possible
Can someone please help me out?
Its explained well in document model form selecting fields to use
You have to do something like this in your view
...
if request.POST:
new_char_form = CreateCharacterForm(request.POST)
if new_char_form.is_valid():
#save form with commit=False
new_char_obj = new_char_form.save(commit=False)
#set user and save
new_char_obj.user = user
new_char_obj.save()
return HttpResponseRedirect('%s/characters/' % user_id)
else:
...