why when I click a different user returns me back to the current request user? - django

sorry, I had to take some screenshots to explains what is going on with me. now, I have much profile that has many different users and when I log in with one of it I'll assume the username is: medoabdin like you find on the screenshot now (medoabdin) and the name of it is (Origin) for me is a current user request. so, now I have also many different questions created by the different users, and when I want to enter any other profile let's suppose the user is (abdelhamedabdin) by current request user it returns me back to the current request (medoabdin) and not returns me back to abdelhamedabdin.
however, when I check the link URL I find the link is correct and when I log in with (abdelhamedabdin) user I see the same thing occurs to me against (medoabdin) so, can anyone tell me what is going on guys?
these are screenshots:
current request (medoabdin),
several questions,
show the link url for different users,
accounts/profile.html
{% extends 'base.html' %}
{% block title %} {{ user.first_name }} {{ user.last_name }} Profile {% endblock %}
{% block body %}
<!-- User Profile Section -->
{% if user.is_authenticated %}
<div class="profile">
<div class="container-fluid">
<div class="col-md-1">
<div class="thumbnail">
<div class="row">
<div class="col-xs-12">
<!-- Profile View Section -->
<div class="logo-image text-center">
{% if user.userprofile.logo %}
<div class="my-image">
{% if request.user.username == user.userprofile.slug %}
<a href="{% url 'accounts:user_image' user.userprofile.slug %}">
<img class="img-responsive" src="{{ user.userprofile.logo.url }}">
</a>
<span>
<a href="{% url 'accounts:add_avatar' user.userprofile.slug %}" class="fa fa-camera fa-1x text-center">
<p>Upload Image</p>
</a>
</span>
{% endif %}
</div>
{% else %}
{% load static %}
<div class="my-image">
<img class="img-responsive img-thumbnail" src="{% static 'index/images/default-logo.jpg' %}">
<span>
<a href="{% url 'accounts:add_avatar' user.userprofile.slug %}" class="fa fa-camera fa-1x text-center">
<p>Upload Image</p>
</a>
</span>
</div>
{% endif %}
{% if user.first_name != '' and user.last_name != '' %}
<h4>{{ user.first_name }} {{ user.last_name }}</h4>
{% else %}
<h4>User Profile</h4>
{% endif %}
</div>
</div>
<div class="col-xs-12">
<div class="caption">
<ul class="nav nav-pills nav-stacked">
<li role="presentation" class="active">Overview</li>
<li role="presentation" class="">Personal Information</li>
<li role="presentation" class="">Skills</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!-- Information Sections -->
<div class="col-md-8 col-md-offset-3 information">
<div class="overview show" id="overview">
<h2 class="line">Overview</h2>
<p class="lead">{{ user.userprofile.overview }}</p>
<a data-placement="bottom" title="update overview" class="fa fa-edit" data-toggle="modal" data-tooltip="tooltip" data-target=".overview_info"></a>
</div>
<div class="personal-info" id="personal-information">
<h2 class="line">Personal Information</h2>
<p class="lead">City: {{ user.userprofile.city }}</p>
<p class="lead">Phone Number: 0{{ user.userprofile.phone }}</p>
<p class="lead">Sex: {{ user.userprofile.sex }}</p>
<a data-placement="bottom" title="update personal information" class="fa fa-edit" data-toggle="modal" data-tooltip="tooltip" data-target=".personal_info"></a>
</div>
<div class="skill" id="my-skills">
<h2 class="line">Skills:</h2>
<p class="lead">{{ user.userprofile.skill }}</p>
<a data-placement="bottom" title="update skills" class="fa fa-edit" data-toggle="modal" data-tooltip="tooltip" data-target=".skills"></a>
</div>
</div>
<!-- get all questions -->
{% if user_prof.userasking_set.all %}
<div class="col-md-8 col-md-offset-3 user_questions">
<h2 class="line">All Questions You Asked</h2>
{% for questions in user_prof.userasking_set.all %}
<p>{{ questions.title }}</p>
{% endfor %}
</div>
{% endif %}
<!-- get favourites -->
{% if get_favourite %}
<div class="col-md-8 col-md-offset-3 user_questions">
<h2 class="line">Favourites</h2>
{% for fav in get_favourite %}
<p>{{ fav.title }}</p>
{% endfor %}
</div>
{% endif %}
</div>
{% include 'accounts/information_form.html' %}
</div>
{% include 'base_footer.html' %}
{% endif %}
{% endblock %}
accounts/views.py
#method_decorator(login_required, name='dispatch')
# view profile page
class ViewProfile(UpdateView):
queryset = UserProfile.objects.all()
template_name = 'accounts/profile.html'
form_class = UpdateInfoForm
slug_field = 'slug'
slug_url_kwarg = 'user_slug'
def get_success_url(self):
return reverse_lazy('accounts:view_profile', kwargs={'user_slug': self.request.user.userprofile.slug})
def get_context_data(self, **kwargs):
self.request.session['switch_comment'] = False
context = super().get_context_data(**kwargs)
user_prof = UserProfile.objects.get(user=self.request.user)
context['user_prof'] = user_prof
context['get_favourite'] = User.objects.get(username=self.request.user.username).favorite.all()
return context
def form_valid(self, form):
form.instance.user_slug = self.request.user.userprofile.slug
self.object = form.save()
return super().form_valid(form)
community/views.py
# List all questions + search
class UserQuestions(ListView):
template_name = 'community/user_questions.html'
context_object_name = 'all_objects'
queryset = UserAsking
def get_context_data(self, object_list=queryset, **kwargs):
context = super().get_context_data(**kwargs)
# paginator
context['all_objects'] = UserAsking.objects.all()
paginator = Paginator(context['all_objects'], 5)
page_number = self.request.GET.get('page_number')
context['all_objects'] = paginator.get_page(page_number)
# search
context['query'] = self.request.GET.get("query", '')
if context['query']:
all_objects = UserAsking.objects.all().order_by('-date')
context['all_objects'] = all_objects.filter(
Q(title__contains=self.request.GET['query']) |
Q(question__contains=self.request.GET['query']) |
Q(field__contains=self.request.GET['query'])
)
return context
community/user_questions.py
{% extends 'base.html' %}
{% block title %} All Questions That People Asked {% endblock %}
{% block body %}
{% if request.user.is_authenticated %}
<div class="all-questions">
<div class="container">
<div class="fl-left hidden-sm hidden-xs">
<h2>All Questions</h2>
</div>
<div class="fl-right hidden-sm hidden-xs">
Ask Question
</div>
<div class="clear"></div>
<div class="row">
<div class="add-q">
<div class="col-sm-12 visible-sm-block visible-xs-block">
<h2>All Questions</h2>
</div>
<div class="col-sm-12 visible-sm-block visible-xs-block">
Ask Question
</div>
</div>
{% if all_objects %}
<div class="col-sm-12">
<div class="questions">
{% for post in all_objects %}
<div class="q_section">
<a class="text-primary title" href="{% url 'community:question_view' post.ask_slug %}">{{ post.title }}</a>
<p class="field">{{ post.field }}</p>
<div class="info fl-right">
<span class="time">{{ post.date }}</span> |
<a href="{% url 'accounts:view_profile' post.userprofile.slug %}" style="font-size:14px">
{% if post.userprofile.user.first_name != '' %}
{{ post.userprofile.user.first_name }}
{% else %}
User
{% endif %}
<img class="logo-image" style="width:25px;height: 25px" src="
{% if request.user.userprofile.logo %}
{{ request.user.userprofile.logo.url }}
{% else %}
{% load static %}
{% static 'index/images/default-logo.jpg' %}
{% endif %}
">
</a>
</div>
<div class="">
<!--a class="btn btn-primary btn-lg" href="{# {% url 'community:delete_post' post.id %} #}">
<i class="fa fa-trash x2"></i>
</a-->
</div>
</div>
{% endfor %}
</div>
</div>
{% else %}
<h2 class="text-center text-info">No Questions</h2>
{% endif %}
</div>
<!-- Pagination -->
{% if all_objects %}
<div class="pagination">
<span class="step-links">
{% if all_objects.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ all_objects.number }} of {{ all_objects.paginator.num_pages }}.
</span>
{% if all_objects.has_next %}
next
last »
{% endif %}
</span>
</div>
{% endif %}
</div>
</div>
{% include 'base_footer.html' %}
{% endif %}
{% endblock %}

Related

django NoReverseMatch at /account/dashboard/1/

I have a problem when I want to visit the user dashboard. On each article there is a link to the user profile, however, when I click on the link I'm getting NoReverseMatch. I was doing troubleshoots for days but I'm not able to fix the problem. Any help on how to fix this error is more than welcome, thank you!
article.html:
<h6> {{ article.author.profile.username}}</h6>
accounts>views:
#login_required
def guest_dashboard(request, pk):
user_other = User.objects.get(pk = pk)
already_followed = Follow.objects.filter(follower = request.user, following = user_other)
if guest_dashboard == request.user:
return HttpResponseRedirect(reverse('dashboard'))
return render(request, 'account/dashboard-guest.html', context = {'user_other' : user_other, 'already_followed' : already_followed})
articles>View:
def article_detail(request, pk):
article = Article.objects.get(pk=pk)
comment_form = CommentForm()
already_liked = Likes.objects.filter(article=article, user=request.user)
likesCounter = Likes.objects.filter(article=article).count()
if already_liked:
liked = True
else:
liked = False
if request.method == 'POST':
comment_form = CommentForm(request.POST)
if comment_form.is_valid():
comment = comment_form.save(commit=False)
comment.user = request.user
comment.article = article
comment.save()
return HttpResponseRedirect(reverse('article_detail', kwargs={'pk':pk}))
return render(request, 'article/single-article.html', context={'article':article, 'comment_form':comment_form, 'liked': liked,'likesCounter':likesCounter,'already_liked':already_liked})
article>URLs:
path('<pk>', article_detail , name = 'article_detail'),
account>URLs:
path('account/dashboard/', dashboard, name = 'dashboard'),
path('account/dashboard/<pk>/', guest_dashboard, name = 'guest_user'),
dashboard-guest.html>
{% extends 'base.html' %}
{% load static %}
{% block content %}
<!-- PROFILE CONTENT -->
<div id="profile-container">
<img class = "profile-cover-image" src = "/media/{{ user_other.profile.cover_picture }}">
<div class="profile-profile-image-cover">
<img class = "profile-profile-image-img" src = "/media/{{ user_other.profile.profile_picture }}">
</div>
<div class="edit-profile">
<div class="social-media-links">
<span><i class="fab fa-facebook-square fa-2x color-fb dec_none"></i></span>
<span><i class="fab fa-linkedin fa-2x color-linked dec_none"></i></span>
<span><i class="fab fa-twitter-square fa-2x color-twitter dec_none"></i></span>
</div>
</div>
<div class="profile-btns">
{% if not already_followed%}
Follow
{% else %}
Unfollow
{% endif%}
{% if user.post_author.all.count > 100 %}
<i class="fas fa-camera-retro fa-4x awards"></i>
{% endif %}
{% if user.article_author.all.count > 100 %}
<i class="fas fa-crown fa-4x awards"></i>
{% endif %}
</div>
</div>
<!-- END OF PROFILE CONTENT-->
<!-- PROFILE STAFF -->
<div class="profile-controllers">
<div class="profile-main-controller">
<div class="containerce">
<p class = 'boxce txt-center'><i class="fas fa-feather-alt fa-2x dcolor"></i>{{ user_other.article_author.all.count }}</p>
<p> Articles </p>
</div>
<div class="containerce">
<p class = 'boxce txt-center'><i class="fas fa-image fa-2x dcolor"></i> {{ user_other.post_author.all.count }}</p>
<p> Posts </p>
</div>
<div class="containerce">
<p class = 'boxce' txt-center><i class="fas fa-mask fa-2x dcolor"></i> {{ user_other.follower.count }}</p>
<p> Following </p>
</div>
<div class="containerce">
<p class = 'boxce txt-center'><i class="fas fa-hand-holding-heart fa-2x dcolor"></i> {{ user_other.following.count }} </p>
<p> Followers </p>
</div>
<div class="containerce">
{% if user_other.article_author.all.count and user_other.article_author.all.count > 100 %}
<p class = 'boxce txt-center'><i class="fas fa-award fa-2x dcolor"></i> 2</p>
{% else %}
<p class = 'boxce txt-center'><i class="fas fa-award fa-2x dcolor"></i> 0</p>
{% endif %}
<p> Awards </p>
</div>
</div>
</div>
<!-- MAIN FUNCTIONS -->
<div class="functions">
<div class="profile-buttons" id = "profile-buttons">
<a id = "btn_articles" href = "#" class = "btn-profile-buttons active">Articles </a>
<a id = "btn_posts" href = "#" class = "btn-profile-buttons ">Posts </a>
<a id = "btn_videos" href = "#" class = "btn-profile-buttons ">Videos</a>
</div>
<div class = "liner"></div>
</div>
<!-- POSTS -->
<div class="container-posts" id = "container_posts">
<!-- Original posts card-->
{% if user_other.post_author.all %}
{% for post in user_other.post_author.all %}
<div class="container-posts-card">
<a href = "{% url 'post_detail' pk=post.pk %}">
<img src = "{{ post.image.url}}" class = "container-post-card-img"> </a>
<div class="posts_card_edit">
<span class = "ellipsis-edit"><i class="fas fa-ellipsis-v"></i></span>
</div>
</div>
{% endfor %}
{% endif %}
<!-- END OF POST CARD-->
</div>
<!--ENDPOSTS-->
<!-- ARTICLES -->
<div class="container-articles" id = "container_articles">
{% if user_other.article_author.all %}
{% for user_other in user.article_author.all %}
<div class="container-article-card">
<a href = "{% url 'article_detail' pk=article.pk %}" class = "article-card-btn">
<img src = "{{ article.image.url }}" class = "container-article-card-img">
<div class="article-card-details">
<h6 class = "container-article-card-title">{{ article.title }}</h6>
<p class = "article-card-created">{{ article.publish_date }}</p>
</div></a>
<div class="container-article-card-edit">
<span class = "ellipsis-edit"><i class="fas fa-ellipsis-h"></i></span>
</div>
</div>
{% endfor %}
{% else %}
<div class="container-article-card">
<p>No Articles</p>
</div>
{% endif %}
</div>
<!-- ENDARTICLES-->
<!-- END OF MAIN FUNCTIONS -->
<br>
{% endblock %}
single-article.html
{% extends 'base.html' %}
{% load static %}
{% load humanize %}
{% load crispy_forms_tags %}
{% block content %}
<!-- CONTENT -->
<div id="container-single-post">
<div class="left-side">
<img class = "single-article-img" src = "{{ article.image.url }}">
<div class="single-post-author">
<img class = 'single-post-author-image' src = "{{ article.author.profile.profile_picture.url}}" style="object-fit:cover">
</div>
</div>
<div class="right-side">
<h6> {{ article.author.profile.username}}</h6>
{% if article.author.profile.full_name %}
<span span = "single-post-att">{{ article.author.profile.full_name}}</span>
{% else %}
<span span = "single-post-att"></span>
{% endif %}
<span span = "single-post-att">{{ article.category }}</span>
<span span = "single-post-att">{{ article.publish_date | naturaltime }}</span>
{%for a in already_liked %}
<div>{{a.user.profile.full_name}}, you are awesome!</div>
{% endfor %}
{% if not liked %}
<span class = "single-post-love" id="probaman"><i class="far fa-heart fa-2x"></i>{{likesCounter}}</span>
{% else %}
<span class = "single-post-love"><i class="fas fa-heart fa-2x"></i>{{likesCounter}}</span>
{% endif %}
</div>
<div class="single-post-content">
<h5>{{ article.title }}</h5>
<p>{{ article.content}}
</p>
</div>
<div class="single-post-comments">
<p>Comments</p>
<!-- Display Comment -->
{% for comment in article.article_comment.all %}
<div class="comment-single">
<div class="comment-author">
<img class = 'single-comment-author-image' src = "{{comment.user.profile.profile_picture.url}}" style="object-fit:cover">
<span class = 'comment-author'>{{comment.user.profile.full_name}}×</span>
</div>
<p class = "comment-date">{{comment.comment_date| naturaltime}}</p>
<div class="comment-author-content">
<p class = "comment-content">{{ comment.comment }}</p>
</div>
</div>
{% endfor %}
<!-- END Display Comment-->
<!-- POST COMMENT -->
<div class="col-lg-12">
<form method = "POST" class = "form-comment">
{{ comment_form | crispy }}
{% csrf_token %}
<button type ='submit' class = 'btn-default-comment'>Comment</button>
</form>
</div>
<!-- END POST COMMENT-->
</div>
</div>
<!-- END OF CONTENT-->
<br>
<br>
{% endblock %}
In the template, you have <a href = "{% url 'article_detail' pk=article.pk %}", but the variable article is not created anywhere. Where should it come from?
There is a loop {% for user_other in user.article_author.all %}, and it seems like it should somehow lead to an article, but that's unclear without the models and business logic knowledge. Should it be {% for article in user.article_author.all %} ?

Image ids apparently not being created

I have created a template for displaying a photo gallery and giving users the ability to add photos to that gallery:
{% extends 'nowandthen/base.html' %}
{% block body_block %}
<br>
<br>
{% if pictures %}
<ul>
{% for p in pictures %}
<div class="container">
<div class="row">
<div class="col-md-8 card mb-4 mt-3 ">
<!-- Card -->
<!-- Card content -->
<div class="card-body d-flex flex-row">
<!-- Content -->
<div>
<!-- Title -->
<h4 class="card-title font-weight-bold mb-2">{{ p.title }}</h4>
<!-- Subtitle -->
<p class="card-text"><i class="far fa-clock pr-2"></i>{{ p.when_added }}</p>
</div>
</div>
<!-- Card image -->
<div class="view overlay">
<img class="card-img-top rounded-0" src="{{ p.image.url }}" alt="Card image cap">
<a href="#!">
<div class="mask rgba-white-slight"></div>
</a>
</div>
<!-- Card content -->
<div class="card-body">
<div class="collapse-content">
<!-- Text -->
<p class="card-text collapse" id="collapseContent">{{ p.description }}</p>
<!-- Button -->
<a class="btn btn-flat red-text p-1 my-1 mr-0 mml-1 collapsed" data-toggle="collapse" href="#collapseContent" aria-expanded="false" aria-controls="collapseContent">Click for description</a>
<i class="fas fa-share-alt text-muted float-right p-1 my-1" data-toggle="tooltip" data-placement="top" title="Share this post"></i>
<i class="fas fa-heart text-muted float-right p-1 my-1 mr-3" data-toggle="tooltip" data-placement="top" title="I like it"></i>
</div>
</div>
<div class="card-body">
<!-- comments -->
<h2>comments</h2>
{% if not p.comments %}
No comments
{% endif %}
{% for x in p.comment %}
<div class="comments" style="padding: 10px;">
<p class="font-weight-bold">
<h4>Comment by</h4> {{ x.user }}
<span class=" text-muted font-weight-normal">
{{ x.created_on }}
</span>
</p>
{{ x.body | linebreaks }}
</div>
{% endfor %}
</div>
<div class="card-body">
{% if new_comment %}
<h2>Your comment has been posted.</h2>
{% else %}
<h3>Leave a comment</h3>
<form action="{% url 'nowandthen:add_comment' p.image_id %}" method="POST">
{{ comment_form.as_p }}
{% csrf_token %}
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
{% endif %}
</div>
</div>
</div>
</div>
<!-- Card -->
{% endfor %}
</ul>
{% else %}
<li><strong>There are no photographs present.</strong></li>
{% endif %}
{% endblock %}
The idea is that there is an image associated with each comment - there is a for loop of {% for p in pictures %} at the start of the page, and I use variations of p. (e.g. p.image_id) to associate particular pictures with particular comments.
In addition, the urls.py contains the following:
path('add_comment/<int:p.image_id>', views.add_comment, name='add_comment')
However, when I run the code, I get an error message that suggests that image ids aren't being created (even though image is a field in he Pictures model I created):
Reverse for 'add_comment' with arguments '('',)' not found. 1 pattern(s) tried: ['add_comment/$']
What do you suggest, please?
EDIT: This is my view:
#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(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 = {'image': image,'comment': comment, 'new_comment': new_comment,'comment_form': comment_form}
return render(request, template_name, context)
And this is my add_comment.html template:
{% extends 'nowandthen/base.html' %}
{% load staticfiles %}
{% block title_block %}
Add self
{% endblock %}
{% block body_block %}
<h1>Add a Comment</h1>
<div>
<form id="comment_form" method="post" action="{% url 'nowandthen:add_comment' comment_id%}" enctype="multipart/form-data" >
{% csrf_token %}
{{ form.as_p }}
<input type="submit" name="submit" value="Add Comment" />
</form>
</div>
{% endblock %}
Since you pictures variable is a Queryset, when you loop through it in your template, to access its ID, you just need to do it that way. instance.id
Based on your case, you will have:
{% url 'nowandthen:add_comment' p.id %}
And you will be able to access the id in your view with image_id

Django : Redirect after post doesn't work

After Create moneylog, I want to got back to moneybook_detail, so I made a moneylog/view.py:
class moneylog_create(CreateView):
form_class = forms.CreateMoneylogForm
template_name = "moneylogs/create.html"
def form_valid(self, form):
moneylog = form.save()
moneybook = moneybook_models.Moneybook.objects.get(
pk=self.kwargs["pk"])
form.instance.moneybook = moneybook
moneylog.save()
form.save_m2m()
return redirect(reverse("moneybooks:detail", kwargs={'pk': moneybook.pk}))
and this is moneybook/urls.py
app_name = "moneybooks"
urlpatterns = [
path("create/", views.moneybook_create.as_view(), name="create"),
path("update/<int:pk>/",
views.moneybook_update.as_view(), name="update"),
path("<int:pk>/", views.moneybook_detail, name="detail")
]
moneylogs/urls.py
app_name = "moneylogs"
urlpatterns = [
path("create/<int:pk>/", views.moneylog_create.as_view(), name="create"),
path("update/<int:pk>/", views.moneylog_update.as_view(), name="update")
]
here is my detail.html
<a style="display:scroll;position:fixed;bottom:50px;right:30px;" href="{% url 'moneylogs:create' pk %}">
<div class="rounded-full h-16 w-16 flex items-center justify-center bg-red-400 text-bold font-bold text-white">+</div>
</a>
here is my moneylogs/forms.py
class CreateMoneylogForm(forms.ModelForm):
class Meta:
model = models.Moneylog
fields = (
"pay_day",
"payer",
"dutch_payer",
"price",
"category",
"memo",
)
widgets = {
"payer": forms.RadioSelect(attrs={"style": "width: 100 %"}),
"dutch_payer": forms.CheckboxSelectMultiple,
"memo": forms.Textarea(attrs={"rows": 3})
}
def save(self, *args, **kwargs):
moneylog = super().save(commit=False)
return moneylog
and moneylog_form.html
<form method="POST" class="w-full" enctype="multipart/form-data">
{% csrf_token %}
{% if form.non_field_errors %}
{% for error in form.non_field_errors %}
<span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.name.label}}
</div>
<div class="w-3/4 border-b my-2 py-3">
{{form.name}}
</div>
</div>
{% if form.name.errors %}
{% for error in form.name.errors %}
<span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.companion.label}}
</div>
<div class="w-3/4 flex inline border-b my-2 py-3">
<div class="w-3/4">
{{form.companion}}
</div>
<div class= "w-1/4 flex justify-center ">
<i class="fas fa-plus-circle"></i>
</div>
</div>
</div>
{% if form.companion.errors %}
{% for error in form.companion.errors %}
><span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.country.label}}
</div>
<div class="w-3/4 border-b my-2 py-3">
{{form.country}}
</div>
</div>
{% if form.country.errors %}
{% for error in form.country.errors %}
<span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.location.label}}
</div>
<div class="w-3/4 border-b my-2 py-3">
{{form.location}}
</div>
</div>
{% if form.location.errors %}
{% for error in form.location.errors %}
<span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.start_date.label}}
</div>
<div class="w-3/4 border-b my-2 py-3">
{{form.start_date}}
</div>
</div>
{% if form.start_date.errors %}
{% for error in form.start_date.errors %}
<span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
<div class="input {% if field.errors %}has_error{% endif %}">
<div class="flex">
<div class="w-1/4">
{{form.end_date.label}}
</div>
<div class="w-3/4 border-b my-2 py-3">
{{form.end_date}}
</div>
</div>
{% if form.end_date.errors %}
{% for error in form.end_date.errors %}
<span class="text-red-700 font-medium text-sm">{{error}}</span>
{% endfor %}
{% endif %}
</div>
<button class="px-2 py-1 rounded bg-red-500 text-white">{{cta}}</button>
</form>
there is no page redirect. no move, it print just log
"POST /moneylogs/create/1/ HTTP/1.1" 200 5275
How can I return to moneybook_detail after create moneylog?? why my redirect doesn't work?
The generic CreateView has two possible return paths when receiving a POST request:
form_valid() which in your case redirects. This is a HTTP status 302.
form_invalid() which renders the template again with the bound form in its context, in order to display the errors. This is a HTTP status 200.
Since your request returns status 200, we can conclude that the view went into the form_invalid() method, meaning your form has errors. It seems you're rendering the various field errors (although the template you're showing is for the wrong form, I assume this is a copy & paste error) but the easiest to debug is add {{ form.errors }} at the top of your template to display all errors. Or set a breakpoint in your form_invalid() method (override it to return super().form_invalid(form)).
either change the app_name or change the href in details.html. Because in one place you mentioned moneylogs and in another place, you mentioned moneybooks.

Django page navigation- home list not visible

I am super new to Dango; pardon me for a rookie question :)
I am learning Django by creating a "Note taking app". This is how the application home page looks.
When I click on any of the notes from the note list, it opens the details on the right-side page. But the problem is it wipes-out the left-hand side note list. To reload the list I need to click on the Home link again. The expected behavior is, it should retain the note-list on the left side as well as show the details on the right frame.
urls.py
from django.urls import path
from .views import NoteListView, NoteDetailView, NoteCreateView, NoteUpdateView, NoteDeleteView
from . import views
urlpatterns = [
path('', NoteListView.as_view(), name='lekha-home'),
path('note/<int:pk>/', NoteDetailView.as_view(), name='note-detail'),
path('note/new/', NoteCreateView.as_view(), name='note-create'),
path('note/<int:pk>/update', NoteUpdateView.as_view(), name='note-update'),
path('note/<int:pk>/delete', NoteDeleteView.as_view(), name='note-delete'),
path('about/', views.about, name='lekha-about'),
]
enter code here
views.py
class NoteListView(ListView):
model = Note
template_name = 'lekha/home.html'
context_object_name = 'notes'
ordering = ['-date_created']
class NoteDetailView(DetailView):
model = Note
# success_url = 'lekha/note_detail.html'
class NoteCreateView(LoginRequiredMixin, CreateView):
model = Note
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
home.html
{% extends "lekha/base.html" %}
{% block content %}
{% for note in notes %}
<div class="list-group">
{{ note.title }}
</div>
{% endfor %}
{% endblock content %}
note_detail.html
{% extends "lekha/base.html" %}
{% block content2 %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-8" href="#">{{ object.author }}</a>
<small class="text-muted">{{ object.date_created|date:"F d, Y" }}</small>
{% if object.author == user %}
<a class="btn float-right btn-secondary ml-1 btn-sm" href="{% url 'note-update' object.id %} ">Update</a>
<a class="btn float-right btn-danger ml-1 btn-sm" href="{% url 'note-delete' object.id %} ">Delete</a>
{% endif %}
</div>
<h4 class="article-title">{{ object.title }}</h4>
<hr>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
{% endblock content2 %}
And this is how I am calling it in base.html
<main role="main" class="container-fluid px-2">
<div class="row">
<div class="col-md-3">
<div class="content-section">
<h4>Notes</h4>
{% block content %}{% endblock %}
</div>
</div>
<div class="col-md-8">
{% block content2 %}{% endblock %}
</div>
</div>
</main>
Sorry for the detailed post. I would appreciate any pointers. Thanks!
Welcome to Django!
Your template note_detail.html extends base.html, which doesn't contain the HTML for the list of notes, and note_detail.html doesn't add the list, so that's why it's not showing up - you haven't added it!
To do this, you need the same {% block content %} from home.html in note_detail.html, and you also need to manually pass a notes context variable to the template. You get that for free with the ListView class.
First, the change to the template:
note_detail.html
{% extends "lekha/base.html" %}
{% block content %}
{% for note in notes %}
<div class="list-group">
{{ note.title }}
</div>
{% endfor %}
{% endblock content %}
{% block content2 %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-8" href="#">{{ object.author }}</a>
<small class="text-muted">{{ object.date_created|date:"F d, Y" }}</small>
{% if object.author == user %}
<a class="btn float-right btn-secondary ml-1 btn-sm" href="{% url 'note-update' object.id %} ">Update</a>
<a class="btn float-right btn-danger ml-1 btn-sm" href="{% url 'note-delete' object.id %} ">Delete</a>
{% endif %}
</div>
<h4 class="article-title">{{ object.title }}</h4>
<hr>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
{% endblock content2 %}
And the change to the view:
views.py
class NoteListView(ListView):
model = Note
template_name = 'lekha/home.html'
context_object_name = 'notes'
ordering = ['-date_created']
class NoteDetailView(DetailView):
model = Note
def get_context_data(self):
data = super().get_context_data
data['notes'] = Note.objects.all().order_by('-date_created')
One last tip: to keep your HTML templates "DRY," you should really extract the list of notes into a separate html template (often called a partial) that you can plug into multiple other templates. Your template setup would look like this:
partials/all_notes.html
{% for note in notes %}
<div class="list-group">
{{ note.title }}
</div>
{% endfor %}
home.html
{% extends "lekha/base.html" %}
{% block content %}
{% include 'lekha/partials/all_notes.html' %}
{% endblock content %}
note_detail.html
{% extends "lekha/base.html" %}
{% block content %}
{% include 'lekha/partials/all_notes.html' %}
{% endblock content %}
{% block content2 %}
<article class="media content-section">
<div class="media-body">
<div class="article-metadata">
<a class="mr-8" href="#">{{ object.author }}</a>
<small class="text-muted">{{ object.date_created|date:"F d, Y" }}</small>
{% if object.author == user %}
<a class="btn float-right btn-secondary ml-1 btn-sm" href="{% url 'note-update' object.id %} ">Update</a>
<a class="btn float-right btn-danger ml-1 btn-sm" href="{% url 'note-delete' object.id %} ">Delete</a>
{% endif %}
</div>
<h4 class="article-title">{{ object.title }}</h4>
<hr>
<p class="article-content">{{ object.content }}</p>
</div>
</article>
{% endblock content2 %}
base.html
<main role="main" class="container-fluid px-2">
<div class="row">
<div class="col-md-3">
<div class="content-section">
<h4>Notes</h4>
{% block content %}{% endblock %}
</div>
</div>
<div class="col-md-8">
{% block content2 %}{% endblock %}
</div>
</div>
</main>

Django fontsize tinymce

I am trying to use django tinymce in my project but the font is too small. I have googled it and learned I am meant to use content_css but without proper step by step approaches as to how exactly this should be done.
I am wondering if there was another way and if there isn't, could someone give me a simple step by step approach to solving it using the content_css.
Below is the forms.py
class PostForm(forms.ModelForm):
text = forms.CharField(help_text='Enter your Post here', widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))
name = forms.CharField(widget=forms.HiddenInput(), initial='User')
created_on = forms.DateTimeField(widget=forms.HiddenInput(), initial=timezone.now())
class Meta:
model = Post
fields = ('title', 'text',)
{% extends 'blog/base.html' %}
{% load staticfiles %}
{% block body_block %}
<!-- <h1>TinyMCE Quick Start Guide</h1>
<form method='post'>
<textarea id = 'mytextarea'>Hello, World!</textarea>
</form> -->
{% if post %}
<div class="single">
<div class="container">
<div class="col-md-8 single-main">
<div class="single-grid">
<h4>{{ post.title|safe }}</h4>
<img src="{% static 'images/post1.jpg' %}" alt=""/>
<p>{{ post.text|safe }}</p>
</div>
<div class="comments">
<h2><u>Comments</u></h2>
{% if comments %}
{% for comment in comments %}
<h3>~{{ comment.commenter.first_name|title}} {{comment.commenter.last_name|title }}</h3>
<ul>
<li>
{{ comment.text|safe }}
</</li><br>
</ul>
<span class="hidden-xs"style="margin-left:70%;, font-family:Arial">Published: {{ comment.created_on }}</span >
{% if comment.commenter == request.user or user.is_superuser %}
<button style="margin-left:90%;,line-height: .9;color: red;, font-family:Arial;" type="button" name="button">Delete</button>
{% endif %}
{% endfor %}
{% else %}
No comments available
{% endif %}
</div>
<div class="content-form">
<h3>Leave a comment</h3>
{% if user.is_authenticated %}
<form id="comment_form" action="{% url 'blog:detail' post.slug %}" method="post">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field }}<br/><br/>
{% endfor %}
<input class="btn btn-primary" type="submit" name="submit" value="submit">
</form>
{% else %}
You must be logged in to comment
{% endif %}
</div>
<ul class="comment-list " >
<h5 class="post-author_head">Written by {{ post.author.first_name|title }} {{ post.author.last_name|title }}</h5>
<li><img src="{% static 'images/avatar.png' %}" class="img-responsive" alt="">
<div class="desc">
<p>View all posts by: {{ post.author.first_name|title }} {{ post.author.last_name|title }}</p>
</div>
<div class="clearfix"></div>
</li>
</ul>
</div>
<div class="col-md-4 side-content">
<div class="recent">
<h3>RECENT POSTS</h3>
{% if recent_posts %}
<ul>
{% for post in recent_posts %}
<li>{{post.title|title }}</li>
{% endfor %}
</ul>
{% else %}
<li>No post has been posted</li>
{% endif %}
</div>
<div class="comments">
<h3>RECENT COMMENTS</h3>
{% if recent_comments %}
<ul>
{% for comment in recent_comments %}
<li>{{comment.commenter|title}} on {{comment.post|title}}</li>
{% endfor %}
</ul>
{% else %}
<li>No comments at the moment</li>
{% endif %}
</div>
<div class="clearfix"></div>
<div class="archives">
<h3>ARCHIVES</h3>
<ul>
<li>October 2013</li>
<li>September 2013</li>
<li>August 2013</li>
<li>July 2013</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
</div>
{% if comment.commenter == request.user or user.is_superuser %}
<button style="margin-left:90%;,line-height: .9;color: red;, font-family:Arial;" type="button" name="button">Delete Post</button>
<button style="margin-left:90%;,line-height: .9;color: red;, font-family:Arial;" type="button" name="button">Edit Post</button>
{% endif %}
{% else %}
asadsh
{% endif %}
{% endblock %}
To add content css files to tinymce, you have to change the tinymce.init object value to include your content_css.
Search for the initialization call in your script and add a line to your object like in this example:
tinymce.init({
...
content_css: [
'//example.com/js/your-css-here.css'
],
...
});
If a content_css part is already present, just add the URL to the array, i.e.
['url 1 here', 'url 2 here', 'your new url here']
In your custom css file, you can now set your desired font size, i.e.
body { font-size: 14px; }