I'm working on this project but I got the following error
comm = doctor_detail.comments.filter(active=True)
AttributeError: 'Doctor' object has no attribute 'comments' however I think that everything is alright
this is my models.py
class Comments(models.Model):
co_name = models.CharField(max_length=50, verbose_name="الاسم ")
co_email = models.EmailField(
max_length=50, verbose_name="البريد الالكتروني")
co_body = models.TextField(max_length=400, verbose_name='التعليق')
created_dt = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=True)
post = models.ForeignKey(
User, on_delete=models.CASCADE, related_name='comments')
def __str__(self):
return 'علق {} على {}'.format(self.co_name, self.post)
this is my views.py
def doctor_detail(request, slug):
# استدعاء جميع البينات اللي في البروفايل
doctor_detail = get_object_or_404(Doctor, slug=slug)
comm = doctor_detail.comments.filter(active=True)
form = Commentair()
if request.method == 'POST':
form = Commentair(data=request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.post = doctor_detail
new_comment.save()
form = Commentair()
else:
form = Commentair()
return render(request, 'user/doctor_detail.html', {'doctor_detail': doctor_detail,
'comm': comm,'form': form
})
I really don't know why I'm facing this error because the related name is available and I think everything is okay .please if there is an answer to this write it and explain it to me. thank you
### You done mistake in here comments -> Comments c will be capital in filter
def doctor_detail(request, slug):
# استدعاء جميع البينات اللي في البروفايل
doctor_detail = get_object_or_404(Doctor, slug=slug)
comm = doctor_detail.Comments.filter(active=True)
form = Commentair()
if request.method == 'POST':
form = Commentair(data=request.POST)
if form.is_valid():
new_comment = form.save(commit=False)
new_comment.post = doctor_detail
new_comment.save()
form = Commentair()
else:
form = Commentair()
return render(request, 'user/doctor_detail.html', {'doctor_detail': doctor_detail,
'comm': comm,'form': form
})
Related
I have a checkbox on my django app, where user can add or remove a listing from their watchlist.
However, this checkbox always returns False, and is never in request.POST, i have tried sooo many solutions from SO and all over the internet for literal days now and cant figure it out
Models.py
class Watchlists(models.Model):
user = models.CharField(max_length=64, default='user')
title = models.CharField(max_length=64, blank=True)
watchlist = models.BooleanField(default=False, blank=False)
def __str__(self):
return f"{self.title}, {self.user}, {self.watchlist}"
Forms.py
class CheckForm(ModelForm):
watchlist = forms.BooleanField(required=False)
# watchlist = forms.DecimalField(widget=forms.CheckboxInput(attrs={"value":"watchlist"}))
class Meta:
model = Watchlists
fields = ['watchlist']
Checkbox didnt have a value so i thought that was the issue and tried to give it one here on the commented line, it didnt help
Views.py
watchlist = CheckForm(request.POST or None)
if request.method == 'POST':
# if request.POST['watchlist']:
# if 'watchlist' in request.POST:
# if request.POST.get('watchlist', False):
if request.POST.get('watchlist', '') == 'on':
if watchlist.is_valid():
check = watchlist.cleaned_data['watchlist']
watchlist_data = Watchlists.objects.all().filter(title=title, user=username).first()
if not watchlist_data:
watchlisted = Watchlists.objects.create(title=title, user=username, watchlist='True')
watchlisted.save()
if watchlist_data:
watchlist_data.delete()
I have tried all the different solutions i could find
**Template**
<form action="listing" method="POST">
{% csrf_token %}
{{ checkbox }}
</form>
It has a name and id attribute, label is fine too
**Entire views.py**
#login_required
def listing(request, title):
if request.user.is_authenticated:
username = request.user.get_username()
form = BidForm()
comment_form = CommentForm()
watchlist = CheckForm(request.POST or None)
listing_object = Listing.objects.all().filter(title=title).first()
author = listing_object.user
bids = Bid.objects.all().filter(title=title).values_list("price", flat=True)
max_bid = max(bids, default=0)
comments = Comment.objects.all().filter(list_title=title)
if request.method == "POST":
bid = Bid(title=title, user=username)
bidform = BidForm(request.POST, request.FILES, instance=bid)
# if request.POST['watchlist']:
# if 'watchlist' in request.POST:
# if request.POST.get('watchlist', False):
if request.POST.get('watchlist', '') == 'on':
if watchlist.is_valid():
check = watchlist.cleaned_data['watchlist']
watchlist_data = Watchlists.objects.all().filter(title=title, user=username).first()
if not watchlist_data:
watchlisted = Watchlists.objects.create(title=title, user=username, watchlist='True')
watchlisted.save()
if watchlist_data:
watchlist_data.delete()
if "price" in request.POST:
if bidform.is_valid():
price = bid.price
if not bids:
bid = bidform.save()
messages.success(request, 'Your bid has been placed succesfully')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
else:
max_bid = max(bids)
if price >= listing_object.price and price > max_bid:
bid = bidform.save()
messages.success(request, 'Your bid has been placed succesfully')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
else:
messages.warning(request, 'Bid price must be greater than highest bid and starting price')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
if "close" in request.POST:
bid = Bid.objects.all().filter(title=title, price=max_bid).first()
max_bid_user = bid.user
listing_object.tag = 'closed'
listing_object.save()
if username == max_bid_user:
messages.warning(request, 'Thank you for your entry into this auction. You have emerged the winner and this listing has been closed')
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
comment = Comment(user_commented=username, list_title=title, list_author=author)
comment_form = CommentForm(request.POST, request.FILES, instance=comment)
if "comment" in request.POST:
if comment_form.is_valid():
user_comment = comment_form.save()
comments = Comment.objects.all().filter(list_title=title)
return HttpResponseRedirect(reverse('listing', args=(), kwargs={'title': title}))
return render(request, "auctions/listing.html", {
"form": form,
"listing": listing_object,
"checkbox": watchlist,
"max_bid": max_bid,
"users": author,
"commentform": comment_form,
"comments": comments
})
urls.py
path("auctions/<str:title>/", views.listing, name="listing"),
First, you don't have to add blank=False in your watchlist field since you gave it a default value, so rewrite it like so
watchlist = models.BooleanField(default=False)
By doing so, you can also remove this from your forms.py. It's not necessary
watchlist = forms.BooleanField(required=False)
Just use as follows
class CheckForm(ModelForm):
class Meta:
model = Watchlists
fields = ['watchlist']
Additional things to fix based on #Sunderam Dubey's comment
In your HTML form, change action="listing" to action="{% url 'listing' %}"
I've this two models:
MODEL
class File(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
filename = models.CharField(max_length=250)
file_upload = models.FileField(upload_to=path)
upload_date = models.DateField(default=datetime.now)
def __str__(self):
return self.user.name + 'file'
class Dataset(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
file_uploaded = models.OneToOneField(File, on_delete=models.CASCADE)
name_user_A = models.CharField(max_length=250)
code_user_A = models.PositiveIntegerField(null=True)
total_user_A = models.PositiveIntegerField(null=True)
sd_user_A = models.PositiveIntegerField(null=True)
name_user_B = models.CharField(max_length=250)
code_user_B = models.PositiveIntegerField(null=True)
total_user_B = models.PositiveIntegerField(null=True)
sd_user_B = models.PositiveIntegerField(null=True)
With File model it should be uploaded a csv file and then the information in the file should be saved in the Dataset model. After that I'd like to show some chart to my user so I need my File and Dataset models linked. This is my view:
VIEWS
def file_upload(request):
data = None
if request.method == 'POST':
form = FileForm(request.POST, request.FILES)
raw_file= request.FILES
if form.is_valid():
form.instance.user = request.user.profile
form.instance.filename = raw_file['file_upload'].name
form.save()
data = request.FILES['file_upload']
data = pd.read_csv(data, header=0, encoding="UTF-8")
data_form.instance.user = request.user.profile
Dataset.objects.create(
name_user_A = data.iloc[0,1],
name_user_B = data.iloc[1,1],
[...]
)
return redirect('upload_file')
else:
return redirect('home')
else:
form = FileForm()
context = {
'data': data,
'form': form,
}
return render(request, 'upload_file.html', context)
When I try to access the Dataset database in the admin area I get this error: 'NoneType' object has no attribute 'user'.
I cannot also access the html page with results and the chart because I'm getting this error: Dataset matching query does not exist.
These are the view and the url code on how I'm reaching the html page
def results(request, id):
results = File.objects.filter(user=request.user.profile).filter(pk=id)
context = {
'results': results
}
return render(request, 'results.html', context)
urlpatterns = [
path('chart/<id>', views.results, name="file"),
]
Can someone kindly help me and explain to me how can I fix my code? Thank you
I am working on a small project containing two models User and Post.And a user can have multiple posts.
I have created a custom form PostForm.
When ever i am submitting the form it showing me the following error:-
'PostForm' object has no attribute 'user'
views.py:-
def add_post(request):
print("addpost runn")
if request.method == 'POST':
post_form = PostForm(request.POST)
print(request.POST)
print("if part")
if post_form.is_valid():
print(post_form)
post_form.save(commit=False)
post_form.user = request.user
post_form.save()
else:
print("else part")
post_form = PostForm()
return render(request, "addpost.html", {"form": post_form})
forms.py:-
class PostForm(forms.ModelForm):
text = forms.CharField(label="Write Your Text Here", max_length=200, widget=forms.Textarea(attrs={'rows': 3}))
created_at = forms.DateTimeField(input_formats=['%d/%m/%Y %H:%M:%S'],
widget=forms.DateTimeInput(format='%d/%m/%Y %H:%M:%S'))
updated_at = forms.DateTimeField(input_formats=['%d/%m/%Y %H:%M:%S'],
widget=forms.DateTimeInput(format='%d/%m/%Y %H:%M:%S'))
class Meta:
model = Post
fields = ['text']
def save(self, commit=True):
print(self.cleaned_data)
post = Post.objects.create_post(
self.cleaned_data['text'],
datetime.datetime.now(),
datetime.datetime.now(),
self.user,
)
return post
manager.py:-
class PostManager(BaseUserManager):
def create_post(self, text, created_at, updated_at, user, **otherfields):
print(created_at)
print(updated_at)
print(text)
post = self.model(text=text, created_at=created_at, updated_at=updated_at, user=user)
post.save(using=self._db)
return post
Here's your answer:
instance=post_form.save(commit=False)
instance.user = request.user
instance.save()
I am currently making a flashcard web application with Django.
There is a 'set' page (dashboard) and a 'card' page (set-edit). When I fill in and submit the form on the card page (set-edit) to add a new card to the set which has been selected for editing, I received a value error ' Cannot assign "2": "Card.set" must be a "Set" instance.'
I'm unsure why this is happening because there is an instance of Set with an id of 2.
Any suggestions of how to rectify this issue?
views.py
###################
##Dashboard views##
###################
def dashboard(request):
set = Set.objects.all()
set_count = set.count()
if request.method == 'POST':
form = SetForm(request.POST)
if form.is_valid():
form.save()
set_name = form.cleaned_data.get('name')
messages.success(request, f'{set_name} has been added')
return redirect('dashboard-dashboard')
else:
form = SetForm()
context = {
'set': set,
'form': form,
}
return render(request, 'dashboard/dashboard.html', context)
#############
##Set views##
#############
#Cards is for when you are adding cards to a set or looking at the content of a set
def set_edit(request, pk):
set_title = Set.objects.get(id=pk)
card = Set.objects.get(id=pk).card_set.all()
set_id = pk
set = Set.objects.get(id=pk)
if request.method == 'POST':
form = CardForm(request.POST)
print('Entered Post condition')
if form.is_valid():
obj = form.save(commit=False)
obj.set = pk
obj.save()
card_name = form.cleaned_data.get('kanji')
messages.success(request, f'{card_name} has been added')
return redirect('dashboard-set-edit',pk)
else:
form = CardForm()
context = {
'card': card,
'form': form,
}
return render(request, 'dashboard/set_edit.html', context)
```
**models.py**
```
class Set(models.Model):
name = models.CharField(max_length=100, null=True)
quantity = models.PositiveIntegerField(null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'{self.id}'
class Card(models.Model):
kanji = models.CharField(max_length=100, null=True)
kana = models.CharField(max_length=100, null=True)
english = models.CharField(max_length=100, null=True)
set = models.ForeignKey(Set, on_delete=models.CASCADE,default=3)
def __str__(self):
return f'{self.id}'
```
**forms.py**
```
class CardForm(forms.ModelForm):
class Meta:
model = Card
# fields = '__all__'
exclude = ('set',)
class SetForm(forms.ModelForm):
class Meta:
model = Set
fields = '__all__'
```
**urls.py**
urlpatterns = [
#########
##Sets###
#########
path('sets/edit/<int:pk>/', views.set_edit,
name='dashboard-set-edit'),
path('sets/delete/<int:pk>/', views.set_delete,
name='dashboard-set-delete'),
#########
##Cards##
#########
path('card/delete/<int:pk>/', views.card_delete,
name='dashboard-cards-delete'),
path('cards/detail/<int:pk>/', views.card_detail,
name='dashboard-cards-detail'),
path('cards/edit/<int:pk>/', views.card_edit,
name='dashboard-cards-edit'),
#############
##Dashboard##
#############
path('dashboard/', views.dashboard, name='dashboard-dashboard'),
]
```
You should assign it to .set_id, not .set:
if form.is_valid():
# use set_id ↓
form.instance.set_id = pk
obj = form.save()
card_name = form.cleaned_data.get('kanji')
messages.success(request, f'{card_name} has been added')
return redirect('dashboard-set-edit',pk)
I trial make Notification for Comment
by Many-to-many relationships
how fix it
raise TypeError(
TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use NotfFaId.set() instead.
[17/Apr/2021 03:37:31] "POST /forum/addcomment/ HTTP/1.1" 500 73061
class CommentT(MPTTModel):
Topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name='comments')
author = models.ForeignKey( User, on_delete=models.CASCADE, related_name='author')
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
NotfFaId = models.ManyToManyField( User, related_name='NotfFaVId', default=None, blank=True)
content = models.TextField()
publish = models.DateTimeField(auto_now_add=True)
status = models.BooleanField(default=True)
class MPTTMeta:
order_insertion_by = ['publish']
def addcomment(request):
if request.method == 'POST':
if request.POST.get('action') == 'delete':
id = request.POST.get('nodeid')
c = CommentT.objects.get(id=id)
c.delete()
return JsonResponse({'remove': id})
else:
comment_form = NewCommentTForm(request.POST)
# print(comment_form)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
result = comment_form.cleaned_data.get('content')
user = request.user.username
user_comment.author = request.user
user_comment.NotfFaId = request.user
user_comment.save()
Topic.objects.get(id = request.POST.get('Topic') ).NotfFaV.add(request.user)
# CommentT.objects.get(id=user_comment.id).NotfFaId.add(request.user)
return JsonResponse({'result': result, 'user': user,'id': user_comment.id })
don't work ,
raise ValueError('"%r" needs to have a value for field "%s" before '
ValueError: "<CommentT: CommentT object (None)>" needs to have a value for field "id" before this many-to-many relationship can be used.
def addcomment(request):
if request.method == 'POST':
if request.POST.get('action') == 'delete':
id = request.POST.get('nodeid')
c = CommentT.objects.get(id=id)
c.delete()
return JsonResponse({'remove': id})
else:
comment_form = NewCommentTForm(request.POST)
# print(comment_form)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
result = comment_form.cleaned_data.get('content')
user = request.user.username
user_comment.author = request.user
user_comment.NotfFaId.set( request.user )
user_comment.save()
Topic.objects.get(id = request.POST.get('Topic') ).NotfFaV.add(request.user)
return JsonResponse({'result': result, 'user': user,'id': user_comment.id })
thank Iain Shelvington,
it is word
I Called CommentT.objects.get(id=user_comment.id).NotfFaId.add(request.user) after user_comment.save(). You can't add many to many entries before you have saved both sides of the relationship – Iain Shelvington 2 hours ago
def addcomment(request):
if request.method == 'POST':
if request.POST.get('action') == 'delete':
id = request.POST.get('nodeid')
c = CommentT.objects.get(id=id)
c.delete()
return JsonResponse({'remove': id})
else:
comment_form = NewCommentTForm(request.POST)
# print(comment_form)
if comment_form.is_valid():
user_comment = comment_form.save(commit=False)
result = comment_form.cleaned_data.get('content')
user = request.user.username
user_comment.author = request.user
# user_comment.NotfFaId.set( request.user )
user_comment.save()
Topic.objects.get(id = request.POST.get('Topic') ).NotfFaV.add(request.user)
CommentT.objects.get(id=user_comment.id).NotfFaId.add(request.user)
return JsonResponse({'result': result, 'user': user,'id': user_comment.id })