add comments

This commit is contained in:
Priyatham-sai-chand 2020-10-19 01:36:31 +05:30
parent 192defed3b
commit a25a71a7e2
16 changed files with 104 additions and 22 deletions

View File

@ -1,8 +1,8 @@
from django.contrib import admin
from .models import User
from .models import AuctionListing
from .models import AuctionListing,Comment
# Register your models here.
admin.site.register(User)
admin.site.register(AuctionListing)
admin.site.register(Comment)

View File

@ -0,0 +1,25 @@
# Generated by Django 3.0.8 on 2020-10-18 16:42
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('auctions', '0003_auctionlisting_date_added'),
]
operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.TextField()),
('date_added', models.DateTimeField(auto_now_add=True)),
('listing', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='auctions.AuctionListing')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='commited_user', to=settings.AUTH_USER_MODEL)),
],
),
]

View File

@ -0,0 +1,23 @@
# Generated by Django 3.0.8 on 2020-10-18 17:34
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('auctions', '0004_comment'),
]
operations = [
migrations.RemoveField(
model_name='user',
name='watchlist',
),
migrations.AddField(
model_name='auctionlisting',
name='watchlist',
field=models.ManyToManyField(blank=True, related_name='watchlist', to=settings.AUTH_USER_MODEL),
),
]

View File

@ -2,7 +2,7 @@ from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
watchlist = models.ManyToManyField('AuctionListing',blank = True,related_name="watchlist")
pass
class AuctionListing(models.Model):
title = models.CharField(max_length = 64,primary_key = True)
@ -12,10 +12,17 @@ class AuctionListing(models.Model):
picture = models.URLField(null=True)
category = models.CharField(max_length = 64)
date_added = models.DateTimeField(auto_now_add=True)
watchlist = models.ManyToManyField(User,blank = True,related_name="watchlist")
def __str__(self):
return f"{self.title}: {self.price},{self.desc},{self.picture},{self.category},{self.date_added}"
class Comment(models.Model):
listing = models.ForeignKey(AuctionListing,on_delete = models.CASCADE,related_name="comments")
user = models.ForeignKey(User,related_name="commited_user",on_delete=models.CASCADE)
body = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.listing.title, self.user)

View File

@ -17,12 +17,14 @@
<p class="card-text" style="font-size: large;">
<b>bid price</b> : ${{listing.price}}<br>
<b>created by</b>: {{listing.user}}
</p>
<b>created by</b>: {{listing.user}}<br>
<small><i> created at {{listing.date_added}}</i> </small>
</p>
<div class = "text-right">
<form action = "{% url 'index' %}" method="POST">
<form action="{% url 'watch' request.user %}" method="POST">
{% csrf_token %}
{{ watch_form }}
<button type="submit" name="watch", value="{{listing.title}}", class="btn btn-primary btn-sm">Watch</button>
</form>

View File

@ -14,5 +14,27 @@
<input class="form-control" autofocus type="number" name="bid" placeholder="Bid">
</div>
<input class="btn btn-primary" type="submit" value="Place Bid">
<h4>Comments ...</h4>
{% if not Listing.comments.all %}
No comments Yet....
{% else %}
{% for comment in Listing.comments.all %}
<strong>{{comment.user}} - {{comment.date_added}}</strong>
<br/>
{{ comment.body }}
<br/>
{% endfor %}
{% endif %}
{% if user.is_authenticated %}
<h5>add comment..</h5>
<form method="POST">
{% csrf_token %}
<input class = "form-control" name="comment_body" autofocus type="text" rows=4 cols= 50 placeholder="Comment here!">
<input class="btn btn-primary" type="submit" value="Submit">
</form>
{% endif %}
{% endblock %}

View File

@ -10,6 +10,7 @@ urlpatterns = [
path("wishlist", views.register, name="wishlist"),
path("create", views.create, name="create"),
path("categories", views.category, name="categories"),
path("category/<str:cat_name>",views.category,name="indiv_categories"),
path("listing/<str:title>",views.listing,name="listing")
path("category/<str:cat_name>",views.category,name="individual_categories"),
path("listing/<str:title>",views.listing,name="listing"),
path("watch/<str:user>",views.watch,name="watchlist")
]

View File

@ -4,12 +4,10 @@ from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django import forms
from .models import User,AuctionListing
from .models import User,AuctionListing,Comment
categories = ['Fashion','Electronics','Home','Sports','Toys','Automobile','Books','Video Games', 'Other']
class Watch_Form(forms.Form):
watch_this = forms.BooleanField(required=False)
class Bid_Form(forms.Form):
bid = 0.0
def __init__(self,bid_value):
@ -20,17 +18,11 @@ class Bid_Form(forms.Form):
def index(request):
listings = AuctionListing.objects.all()
watch_form = Watch_Form(request.POST)
if watch_form.is_valid():
print("asdf")
watch = watch_form.cleaned_data["watch_this"]
print(watch)
if request.method == "POST":
return render(request, "auctions/index.html",{
"Listings":listings,
"watch_form": Watch_Form()
})
def listing(request,title):
@ -38,9 +30,15 @@ def listing(request,title):
docstring
"""
listings = AuctionListing.objects.get(pk = title)
if request.method == "POST":
body = request.POST["comment_body"]
comment = Comment(user=request.user,listing=listings,body = body)
comment.save()
return render(request,"auctions/listing.html",{
"Listing": listings
})
def category(request,cat_name = None):
"""
docstring
@ -61,6 +59,7 @@ def category(request,cat_name = None):
return render(request,"auctions/category_listing.html",{
"categories": categories
})
def login_view(request):
if request.method == "POST":
@ -128,3 +127,6 @@ def create(request):
return render(request,"auctions/create_listing.html",{
"categories":categories
})

Binary file not shown.