django-views

How can I display a user profile using Django? [closed]

不羁的心 提交于 2019-11-30 10:27:32
I am new to django and I am currently trying to build a website that allows users to sign in and view other users profiles. So far I have managed to let users sign in but I can't work out how to view other peoples profiles. Each profile uses the users username to create a url for their profile. Currently if I sign in as one user and change the URL to another users profile URL, it still displays the current users profile. I want something similar to pinterest where any person whether they are signed in or not can view peoples profiles. Any help would be appreciated! View from django.http import

How to compare datetime in Django?

独自空忆成欢 提交于 2019-11-30 10:10:31
Suppose I have: ds = datetime.datetime.now dd = Entry.objects.get(pk=id).pub_date How to compare 2 objects above? I want to get the time difference between them. Please help me solve this problem. Thank you very much ! I am assuming that pub_date is a django.db.models.DateField , which means you can treat it as a datetime.date object. If you convert them to the same type (either datetime.datetime or datetime.date ) and subtract one from the other, you will get an instance of datetime.timedelta . As you are using datetime.datetime.now() , if your pub_date is simply a date rather than a datetime

Using Django class-based views, how can I return a different template if request.is_ajax

徘徊边缘 提交于 2019-11-30 09:56:10
I find Django's request.is_ajax a very useful way to add progressive enhancement via JS and still keep DRY in my views. However, I want to use class-based views and render with a different template if request.is_ajax. It is not clear to me how I can override my default "template_name" and make the template loading conditional in class-based views. How can I do this? The appropriate way to do this is to override the methods provided by the TemplateResponseMixin . If you simply need to provide a different template for Ajax requests, then override get_template_names . If you want to provide a

Saving Django ModelForm with a ForeignKey

我只是一个虾纸丫 提交于 2019-11-30 09:36:13
This is probably a fairly simple question, but I can't seem to figure it out from the Django Docs. I'm trying to save a two ModelForms at once with one being a ForeignKey of another. I'm not sure how to write the logic in the views to ensure these go together properly. models.py class Address(models.Model): address = models.CharField(max_length=100) city = models.CharField(max_length=50) zipcode = models.PositiveIntegerField() class Store(models.Model): name = models.CharField(max_length=100) description = models.CharField(max_length=140, blank=True) address = models.ForeignKey(Address, null

Django check if object in ManyToMany field

允我心安 提交于 2019-11-30 07:48:40
I have quite a simple problem to solve. I have Partner model which has >= 0 Users associated with it: class Partner(models.Model): name = models.CharField(db_index=True, max_length=255) slug = models.SlugField(db_index=True) user = models.ManyToManyField(User) Now, if I have a User object and I have a Partner object, what is the most Pythonic way of checking if the User is associated with a Partner? I basically want a statement which returns True if the User is associated to the Partner . I have tried: users = Partner.objects.values_list('user', flat=True).filter(slug=requested_slug) if

'RelatedManager' object is not iterable Django

倖福魔咒の 提交于 2019-11-30 07:43:59
问题 Hey i have looked around through some simliar posts here on SO but havent found anything that has solved my problem. I have the following models, from django.db import models class Areas(models.Model): name = models.CharField(max_length = 120) order_in_sidebar_network = models.IntegerField(blank=True, null=True) order_in_section_network = models.IntegerField(blank=True, null=True) def __unicode__ (self): return self.area_name class Meta: verbose_name_plural = "Areas" verbose_name = "Area"

__init__() got an unexpected keyword argument 'user'

一笑奈何 提交于 2019-11-30 06:39:27
i am using Django to create a user and an object when the user is created. But there is an error __init__() got an unexpected keyword argument 'user' when calling the register() function in view.py. The function is: def register(request): '''signup view''' if request.method=="POST": form=RegisterForm(request.POST) if form.is_valid(): username=form.cleaned_data["username"] email=form.cleaned_data["email"] password=form.cleaned_data["password"] user=User.objects.create_user(username, email, password) user.save() return HttpResponseRedirect('/keenhome/accounts/login/') else: form = RegisterForm()

django - get() returned more than one topic

99封情书 提交于 2019-11-30 06:33:29
问题 When I tried to relate an attribute with another one which has an M to M relation I received this error: get() returned more than one topic -- it returned 2! Can you guys tell me what that means and maybe tell me in advance how to avoid this error ? models class LearningObjective(models.Model): learning_objective=models.TextField() class Topic(models.Model): learning_objective_topic=models.ManyToManyField(LearningObjective) topic=models.TextField() output of LearningObjective.objects.all() [

Raw SQL queries in Django views

老子叫甜甜 提交于 2019-11-30 06:21:56
问题 How would I perform the following using raw SQL in views.py ? from app.models import Picture def results(request): all = Picture.objects.all() yes = Picture.objects.filter(vote='yes').count() return render_to_response('results.html', {'picture':picture, 'all':all, 'yes': yes}, context_instance=RequestContext(request)) What would this results function look like? 回答1: >>> from django.db import connection >>> cursor = connection.cursor() >>> cursor.execute('''SELECT count(*) FROM people_person''

The view didn't return an HttpResponse object. It returned None instead

て烟熏妆下的殇ゞ 提交于 2019-11-30 05:58:38
I have the following simple view. Why is it resulting in this error? The view auth_lifecycle.views.user_profile didn't return an HttpResponse object. It returned None instead. """Renders web pages for the user-authentication-lifecycle project.""" from django.shortcuts import render from django.template import RequestContext from django.contrib.auth import authenticate, login def user_profile(request): """Displays information unique to the logged-in user.""" user = authenticate(username='superuserusername', password='sueruserpassword') login(request, user) render(request, 'auth_lifecycle/user