django-views

Django Indentation error

非 Y 不嫁゛ 提交于 2019-12-07 19:25:32
问题 I am new to Django and was trying this code in a tutorial. But now I'm not able to run my program because of the following error: IndentationError at / ('unexpected indent', ('D:\\django_workspace\\django_bookmarks\\..\\django_bookmarks\\bookmarks\\views.py', 14, 4, ' return HttpResponse(output)\n')) Request Method: GET Request URL: http://localhost:8000/ Exception Type: IndentationError Exception Value: ('unexpected indent', ('D:\\django_workspace\\django_bookmarks\\..\\django_bookmarks\

Adding Foreign Key To Django Model

情到浓时终转凉″ 提交于 2019-12-07 18:30:26
I was wondering what they best procedures in turning a column from an IntegerField to a Foreign Key. Here are my two models: class workout(models.Model): userid = models.IntegerField() datesubmitted = models.DateField() workoutdate = models.DateField(); bodyweight = models.FloatField(null=True); totalreps = models.IntegerField() totalweight = models.FloatField() numsets = models.IntegerField(); numexercises = models.IntegerField() workoutname = models.CharField(max_length=250) and the second one: class exercise(models.Model): userid = models.IntegerField(); workoutid = models.IntegerField();

django sort the query elements in a weekly monthly daily fashion

混江龙づ霸主 提交于 2019-12-07 18:18:16
问题 I am getting a list of people and the tests they have taken from an api in the same project. I would like the user to have an option to see the number of tests that have taken place in a city with three options - daily/weekly/monthly. models.py class City(models.Model): city_name=models.CharField(max_length=100,default='',blank=False) class Person(models.Model): title = models.CharField(max_length=3,default="mr",blank=False) name = models.CharField(max_length=50,default='',blank=False)

How to make infinite threaded comments

眉间皱痕 提交于 2019-12-07 18:03:24
My current code allows me to render a queryset of Comments (parent comments) as well as replies to those comments. But i'm unable to render replies to those replies. My goal is to have an infinite reply system. Here's my code: class Comment(models.Model): user = models.ForeignKey(User, blank=True, null=True) destination = models.CharField(default='1', max_length=12, blank=True) parent_id = models.IntegerField(default=0) parent_comment = models.ForeignKey('self', related_name='replies', related_query_name='replies', blank=True, null=True) comment_text = models.TextField(max_length=350, blank

Django could not load template tag

最后都变了- 提交于 2019-12-07 17:54:11
问题 I have created a templatetags folder inside my application and inside a file named posts.py , I have written the following code; from django.template import Library, Node from advancedviews.models import Post register = Library() class AllPost(Node): def render(self,context): context['all_posts'] = Post.objects.all() return '' def get_all_posts(parser,token): return AllPost() get_all_posts = register.tag(get_all_posts) Now, I try to load this template tag inside my template; {% load get_all

Query of a subquery in Django

﹥>﹥吖頭↗ 提交于 2019-12-07 17:12:01
问题 I'm trying to do a query from another query, but Django said: 'Caught DatabaseError while rendering: subquery returns more than 1 row.' I'm using PostGis. my model class Place(models.Model): coordinate = models.PointField() class TranslatedPlace(models.Model): place = models.ForeignKey(Place) my view near_coordinates = Place.objects.filter(coordinate__distance_lte=(place_obj.coordinate, D(km=100))) near_places = TranslatedPlace.objects.filter(place=near_coordinates) 回答1: I believe you'll want

Django CBV: Easy access to url parameters in get_context_data()?

大兔子大兔子 提交于 2019-12-07 17:09:20
问题 I understand that the standard way of accessing named url parameters in a custom get_context_data() method is through self.kwargs . However, the self.kwargs syntax becomes awkward, especially when handling a significant number of parameters. So, I've been resorting to something like this at the top of every get_context_data() method -- just to get easy-to-handle local variables: def get_context_data(self, **kwargs): var1, var2, var3, var4, var5 = [self.kwargs[x] for x in ['var1', 'var2',

Django: validating unique_together constraints in a ModelForm with excluded fields

我与影子孤独终老i 提交于 2019-12-07 16:15:13
问题 I have a form: class CourseStudentForm(forms.ModelForm): class Meta: model = CourseStudent exclude = ['user'] for a model with some complicated requirements: class CourseStudent(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL) semester = models.ForeignKey(Semester) block = models.ForeignKey(Block) course = models.ForeignKey(Course) grade = models.PositiveIntegerField() class Meta: unique_together = ( ('semester', 'block', 'user'), ('user','course','grade'), ) I want the new

Django 1.11 404 Page while Debug=True

时间秒杀一切 提交于 2019-12-07 15:24:39
问题 Without making things difficult , I just want to show a special 404 render with staticfiles. If you set DEBUG = False you can use in urls.py handler404 = 'app.views.handler404' But it is without staticfiles. I don't want to install a web server for a simple app. With DEBUG = True in urls url(r'^404/$', views.handler400) is not overriding the default Page not found (404) page. What is the easy way to achieve a render e.g. when you type localhost/asdfhjfsda with staticfiles when DEBUG=True ?

Passing request.user to template in Django

蓝咒 提交于 2019-12-07 14:47:57
问题 Is there another way to get the request.user by not passing it from the views? I think passing request.user from all functions in views to the template is quite wrong. Is there any method or way that the template will get the user or any object in the database? 回答1: By default (I am talking about Django version 1.3) you do not need change TEMPLATE_CONTEXT_PROCESSORS. Because default value already contains *django.contrib.auth.context_processors.auth*. So to your question: By default, you