Populate script not working
I've created a population script to test my website - please see below - but I get the following error message: django.contrib.auth.models.DoesNotExist: User matching query does not exist. Given that I've created a hypothetical user, I'm not sure why this is? Any suggestions would much appreciated. Thank you. Jeff EDIT : I have done both makemigrations and migrate. import os import django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mynowandthen.settings') django.setup() from django.contrib.auth.models import User from nowandthen.models import Picture, Comment, UserProfile def populate(): banana_user = [{'username': 'Banana','email': 'ban...@cat.com', 'password': 'Banana1234'},] johann_user = [{'username': 'Banana', 'email': 'ban...@cat.com', 'password': 'Banana1234'},] wmffre_user = [{'username': 'Wmffre', 'email': 'wmf...@cat.com', 'password': 'Wmffre1234'},] maryhill_comments = [{'user': banana_user, 'body': 'wowo I love this photo - reminds me of when I used to live there!'}, {'user': johann_user,'body': 'I love Maryhill - its such a pretty part of Glasgow lol'}, {'user': wmffre_user,'body': 'gonnae no dae that lol', 'user_id': 3}] fireworks_comments = [{'user': banana_user,'body': 'amazing fireworks - thanks for sharing :)'}, {'user': johann_user,'body': 'love fireworks, love this, love YOU!'}, {'user': wmffre_user,'body': 'whoop!'}] cityscape_comments = [{'user': banana_user,'body': 'more pics like this one please!!'}, {'user': johann_user,'body': 'what a sucky picture hahah'}, {'user': wmffre_user,'body': 'great - love it!'}] pics = {'Maryhill': {'comments': maryhill_comments, 'image': 'shared_pics/View-from-kitchen-window-of-Maryhill-tenements.1970.jpg', 'title': 'Maryhill Laundry', 'description': 'back view', 'tag_one': 'Maryhill', 'tag_two': 'Laundry', 'era': '1970s', 'likes': 64}, 'Fireworks': {'comments': fireworks_comments, 'image': 'shared_pics/fireworks.jpg', 'title': 'Glasgow Fireworks', 'description': 'Fireworks at Glasgow Green', 'tag_one': 'Glasgow', 'tag_two': 'Fireworks', 'era': '2010s', 'likes': 32}, 'Cityscape': {'comments': cityscape_comments, 'image': 'shared_pics/glasgow_cityscape_copy.jpg', 'title': 'Glasgow Cityscape', 'descrpition': 'View over Glasgow', 'tag_one': 'Cityscape', 'tag_two': 'Glasgow', 'era': '1990s', 'likes': 16}} for pic, pic_data in pics.items(): p = add_picture(pic, pic_data['image'], pic_data['title'], pic_data['description'], pic_data['tag_one'], pic_data['tag_two'], pic_data['era']) for c in pic_data['comments']: add_comment(c[0], c[1]) for p in Picture.objects.all(): for c in Comment.objects.filter(picture=p): print(f' - {p}: {c}') def add_user(name, email, password): u = User.objects.get_or_create(username=name, first_name='Test', last_name='User', email=email)[0] u.set_password(password) up = UserProfile.objects.get_or_create(user=u)[0] u.save() return up def add_picture(image, title, description, tag_one, tag_two, era, likes=0): p = Picture.objects.get_or_create(image=image, title=title)[0] p.description = description p.tag_one = tag_one p.tag_two = tag_two p.era = era p.like = likes p.save() return p def add_comment(user, body): u = User.objects.get(username=user) up = UserProfile.objects.get(user=u) c = Comment.objects.get_or_create(user=up, body=body)[0] c.save() return c if __name__ == '__main__': print('Starting Now And Then population script...') populate() -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d5bfae89-4351-4760-9539-923c1b398a13%40googlegroups.com.
Re: Associating comments with images
Thanks Andreas When I do that, I get an error message: NoReverseMatch at /photo_feed/ Could the problem be with my add_comment.html? The code for that is as follows: {% extends 'nowandthen/base.html' %} {% load staticfiles %} {% block title_block %} Add self {% endblock %} {% block body_block %} Add a Comment {% csrf_token %} {{ form.as_p }} {% endblock %} -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/493eaf33-0918-4f6c-9dcb-59465ff2c616%40googlegroups.com.
Re: Associating comments with images
Thank you Andreas. This is the views.py - https://github.com/EmilyQuimby/my_now_and_then/blob/master/nowandthen/views.py -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/944c9708-34f5-497a-a877-ecd66cd0fbca%40googlegroups.com.
Re: Associating comments with images
Thanks Andreas I think that one of the mistakes I'd made previously was to not associate particular comments with particular pictures. I've tried to rectify that using the code below. However, when I do this, the page I get is empty. Any advice would be appreciated: {% extends 'nowandthen/base.html' %} {% block body_block %} {% if pictures %} {% for p in Picture.objects.all %} {{ p.title }} {{ p.when_added }} {{ p.description }} Click for description comments {% if not p.comments %} No comments {% endif %} {% for x in p.comment %} Comment by {{ x.user }} {{ x.created_on }} {{ x.body | linebreaks }} {% endfor %} {% if new_comment %} Your comment has been posted. {% else %} Leave a comment {{ comment_form.as_p }} {% csrf_token %} Submit {% endif %} {% endfor %} {% else %} There are no photographs present. {% endif %} {% endblock %} Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7a56b57b-42e9-42be-bc9f-3ed8714cf444%40googlegroups.com.
Re: NoReverseMatch error message - Please help
Thanks Ryan. I've just tried that, but I still get an error message. By the way, is it definitely .id and not _id? I've seen both, and Django docs says: 'Behind the scenes, Django appends "_id" to the field name to create its database column name, which makes me wonder if it might be _id. Incidentally, I've also tried amending the relevant URL path to path('add_comment/', views.add_comment, name='add_comment') - with underscore and with a dot before the id - but that doesn't work. Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4473f031-abfa-428a-8804-debaf5a41c93%40googlegroups.com.
Associating comments with images
I am creating a website where users can comment on photos in a gallery. The challenge I currently have is that Django is not able to determine which of the photos the comment 'submit' button relates to. This is my HTML code for the photo gallery: comments {% if not comments %} No comments {% endif %} {% for x in comment %} Comment by {{ x.user }} {{ x.created_on }} {{ x.body | linebreaks }} {% endfor %} {% if new_comment %} Your comment has been posted. {% else %} Leave a comment {{ comment_form.as_p }} {% csrf_token %} Submit {% endif %} How do you suggest I overcome this problem, please? Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cabe2b94-bf00-4394-b699-8a15d48247d2%40googlegroups.com.
NoReverseMatch error message - Please help
I am putting together a website which has a photo gallery where users can add comments. When I go to the photo gallery page, I get the following error message: NoReverseMatch at /photo_feed/ Reverse for 'add_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['add_comment/$'] The code for the relevant part of the HTML document is as follows: comments {% if not comments %} No comments {% endif %} {% for x in comment %} Comment by {{ x.user }} {{ x.created_on }} {{ x.body | linebreaks }} {% endfor %} {% if new_comment %} Your comment has been posted. {% else %} Leave a comment {{ comment_form.as_p }} {% csrf_token %} Submit {% endif %} The URLs.py entry for add_comment is path('add_comment/', views.add_comment, name='add_comment'). Removing the int: image_id doesn't fix the problem. When I go into admin, no ids appear to have been generated for the photos. Could it be that the problem is that there are missing IDs? If so, how do I fix this? The repository URL is https://github.com/EmilyQuimby/my_now_and_then. Thank you. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bb078990-f0e1-484a-a3dc-366f018b1df6%40googlegroups.com.
Re: HTML code not being read
I've modified my code a bit to try (unsuccessfully!) to get this working. My views.py is now as follows: @login_required def add_comment(request, image_id): new_comment = None template_name = 'add_comment.html' image = get_object_or_404(Picture, id=image_id) comment = image.comments.filter(active=True) new_comment = None # Comment posted if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): # Create Comment object and don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() context = {'image': image,'comment': comment, 'new_comment': new_comment,'comment_form': comment_form} return render(request, template_name, context) My html code for the gallery is now: comments {% if not comments %} No comments {% endif %} {% for comment in comment %} Comment by {{ comment.user }} {{ comment.created_on }} {{ comment.body | linebreaks }} {% endfor %} The problem I now have is that I now get the following error message: Reverse for 'add_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['add_comment/(?P[0-9]+)$'] Any suggestions would be much appreciated. Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e2212e12-d379-4bcb-8c96-eff4c89c5245%40googlegroups.com.
Re: HTML code not being read
Thanks guys I previously had 'comments' in the plural in views.py, but had the same problem. Am I right in thinking that Django recognises that 'comments' is the plural of 'comment', so if it knows from the view what a comment is, it knows what comments are? Thanks, Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/055a99d8-a18f-4235-b133-feb9dd206930%40googlegroups.com.
Re: HTML code not being read
Hi I'm not sure what you mean by 'Have you all ready use a content that define your content'. I'm new to Django, so am not familiar with all of the terminology yet. Can you elaborate please? Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5e97ab38-2663-4943-b4b8-bf6e3ea744a5%40googlegroups.com.
Re: HTML code not being read
Hi Thanks for getting back to me. This is from my views.py: @login_required def add_comment(request, image_id): new_comment = None template_name = 'add_comment.html' image = get_object_or_404(Picture, id=image_id) comment = image.comment.filter(active=True) new_comment = None # Comment posted if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create Comment object and don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() context = {'comment_form': comment_form, 'image': image,'comment': comment, 'new_comment': new_comment,'comment_form': comment_form} return render(request, template_name, context) Does that look correct to you? Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7f92c0a3-07cc-4b33-ba37-e32bc24c5386%40googlegroups.com.
HTML code not being read
Hi I have written some code that allows users of a website to comment on photos in a picture gallery, using a form. However, when I test the code, no comments are displayed. It would appear that Django is not processing the following code (from my HTML file for the photo gallery), given that 'Comment by' is not displayed on screen: {% for comment in comments %} Comment by {{ comment.user }} {{ comment.created_on }} {{ comment.body | linebreaks }} {% endfor %} Does anyone have any suggestions as to how I can fix this, please? Thank you. Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4666d2ec-e4d8-492d-8203-03dfecd29d6a%40googlegroups.com.
Re: Form not displaying
Will do, thanks -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0969e788-8cea-40a0-acc8-655bfa8ab2ce%40googlegroups.com.
Re: Form not displaying
Thanks Ernest. I'm not sure I follow (forgive me, I am very new to Django). Given that template_name is defined earlier in the method as 'add_comment.html', surely what I've written is equivalent to: return render(request, add_comment.html, {'image': image, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form}) What should the code be instead? Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8e5fcc67-5a91-4371-a51a-9860a6cc78e3%40googlegroups.com.
Re: Form not displaying
Sure. It's as follows: @login_required def add_comment(request, image_id): template_name = 'add_comment.html' image = get_object_or_404(Picture, id=image_id) comments = image.comments.filter(active=True) new_comment = None # Comment posted if request.method == 'POST': comment_form = CommentForm(data=request.POST) if comment_form.is_valid(): # Create Comment object and don't save to database yet new_comment = comment_form.save(commit=False) # Assign the current post to the comment new_comment.post = post # Save the comment to the database new_comment.save() else: comment_form = CommentForm() return render(request, template_name, {'image': image, 'comments': comments, 'new_comment': new_comment, 'comment_form': comment_form}) Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8e0f5a28-7a48-4037-aba5-ea591a81d397%40googlegroups.com.
Form not displaying
Apologies if I'm posting this twice - my original message isn't displayed in the list of messages. I am creating a website using Django. I'd like to give users the chance to comment on pictures they have posted. I have created a comment model and a comment form, and put the following code into the HTML document for the photo gallery: Leave a comment {{ comment_form.as_p }} {% csrf_token %} Submit s. return self._html_output( normal_row='%(field)s', error_row='%s', row_ender='', help_text_html=' %s', errors_on_separate_row=True)` So does my model in models.py: class Comment(models.Model): COMMENT_MAX_LENGTH = 1000 image = models.ForeignKey(Picture, on_delete=models.CASCADE, related_name="comments") user = models.ForeignKey(UserProfile, on_delete=models.CASCADE) body = models.TextField(max_length=COMMENT_MAX_LENGTH) created_on = models.DateTimeField(auto_now_add=True) active = models.BooleanField(default=False) class Meta: ordering = ['created_on'] def __str__(self): return 'Comment {} by {}'.format(self.body, self.user) When I go into the source code on the site, it shows that the form is hidden. Also, when I change comment_form.as_p to something random, no error messages are generated. It's like something is causing Django to skip past that bit of code. I'd really appreciate any suggestions anyone could please offer. Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac7ac525-9bc4-4043-a142-b97b1d3d0e5c%40googlegroups.com.
Form not being published
I am creating a website using Django. I'd like to give users the chance to comment on pictures they have posted. I have created a comment model and a comment form, and put the following code into the HTML document for the photo gallery: Leave a comment {{ comment_form.as_p }} {% csrf_token %} Submit However, the form is not displaying - there is nothing between 'Leave a comment' and the submit button on the page. I don't understand this as my form in forms.py appears to be configured correctly: class CommentForm(forms.ModelForm): body = forms.CharField(help_text="What is your comment?", widget=forms.TextInput(attrs={'size': '1000'}), required=True) class Meta: model = Comment fields = ('body',) def as_p(self): # Returns this form rendered as HTML s. return self._html_output( normal_row='%(field)s', error_row='%s', row_ender='', help_text_html=' %s', errors_on_separate_row=True)` So does my model in models.py: class Comment(models.Model): COMMENT_MAX_LENGTH = 1000 image = models.ForeignKey(Picture, on_delete=models.CASCADE, related_name="comments") user = models.ForeignKey(UserProfile, on_delete=models.CASCADE) body = models.TextField(max_length=COMMENT_MAX_LENGTH) created_on = models.DateTimeField(auto_now_add=True) active = models.BooleanField(default=False) class Meta: ordering = ['created_on'] def __str__(self): return 'Comment {} by {}'.format(self.body, self.user) When I go into the source code on the site, it shows that the form is hidden. Also, when I change comment_form.as_p to something random, no error messages are generated. It's like something is causing Django to skip past that bit of code. I'd really appreciate any suggestions anyone could please offer. Thanks Jeff -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4be7728e-ef60-45a9-8a35-1920ee5d5143%40googlegroups.com.