I have the following html that the button does a normal Save:
<form action="" method="post">
<input type="hidden" value="{{ people }}" name="people">
<input type="hidden" value="{{ number }}" name="number">
<p>{% trans 'Are you sure you want to save people' %} {{ people }} {% trans 'where number is' %} {{ number }}?
</p>
{% buttons %}
<button class="btn btn-default hover-linea" id="cancel" type="button">
{% bootstrap_icon "remove" %} {% trans 'Cancel' %}
</button>
<button type="submit" class="btn btn-default hover-linea">
{% bootstrap_icon "log-out" %} {% trans 'Save' %}
</button>
{% endbuttons %}
What I need when I do the Save, is to reload the previous page to the form. I want to press Save and do the same as now, but also make a location.reload() of a page whose relative link is (../people.html).
I await an answer, thank you very much.
Your server-side form handler (which you have specified the URL to in your action) attribute should respond with an HTTP redirect (status 302 and the Location: header) to the URL you want the browser to load.
Related
I want to receive users that a user has chosen to create a group chat. In an Aiohttp to get variable from HTML select tag, I use self.request.post()['variablename']:
#login_required
async def post(self):
data = await self.request.post()
chat_topic = data.get('chat_topic', '').lower()
users = data['users']
await Chat.create(topic=chat_topic, users=group_users)
This is my rooms.html:
{% extends 'base.html' %}
{% block content %}
<form class="form-signin" method="POST" action="{{ app.router['create_chat'].url_for() }}">
<h2 class="form-signin-heading">Create new chat</h2>
<label for="chat_topic" class="sr-only">chat topic</label>
<input name="chat_topic" type="text" id="chat_topic" class="form-control" maxlength="32" placeholder="chat topic" required autofocus>
<label for="users"> Choose users: </label>
<select name="users" id="users" multiple="multiple">
{% for user in users %}
<option value="{{ user.id }}">
{{ user.username }}
</option>
{% endfor %}
</select>
<button class="btn btn-lg btn-primary btn-block" type="submit">create chat</button>
</form>
{% endblock %}
Unfortunately, I receive only the last selected user as a string, not all of selected users. How can I get an array of selected users in aiohttp from an HTML select tag?
I know that in Django, I could do something like this:
users = request.POST.getlist('users')
I will appreciate any help!
Hello I have a post blog where I have a homepage with posts and if you click on it you are redirected to the post_detail page. Now I want the users to allow to delete their comments which I achieved but I want them to stay on the same post_detail page. I could not come over it and hope that someone can help me. Thanks in advance.
post_detail.html
<div class="container">
<br>
<h1 style="text-align:center;">{{post.post}} </h1>
<p style="font-size:small;text-align:center;">{{post.created_on}}</p>
<button type="button" class="btn btn-secondary float-right" data-toggle="modal" data-target="#exampleModal" data-whatever="#mdo">Yorum At</button>
<br>
<br>
<br>
{% for i in commentmodel_list%}
<h4 >{{i.comment}}</h4>
{% if i.author_security == user%}
<a style="float:right;color:red;"href="{% url 'forum:delete' slug=post.slug comment_id=i.id %}"><i class="fas fa-minus-circle"></i></a>
{% endif %}
{% if i.author is none %}
<p style="font-size:small;">Anonim | {{i.created_on}}</p>
{% else%}
<p style="font-size:small;"><a style="text-decoration:none;" href="#">#{{i.author}}</a> | {{i.created_on}}</p>
{% endif %}
<hr>
<br>
{% endfor %}
</div>
urls.py
app_name='forum'
urlpatterns=[
path('', views.PostList.as_view(), name='post'),
path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),
path('delete/<slug:slug>/<comment_id>/',views.delete_post,name='delete')
]
views.py
def delete_post(request,slug=None,comment_id=None):
comment_delete=Comment.objects.get(id=comment_id)
comment_delete.delete()
post = Post.objects.get(slug=slug)
return redirect('forum:post_detail', slug= post)
I would highly recommend to delete the comments via submitting a POST request with CSRF token, otherwise it's prone to CSRF attack. When you'll be submitting the form, you can add there an input with a value representing the current page which you'll redirect to afterwards. Pseudocode:
<form action="{% url 'forum:delete' slug=post.slug comment_id=i.id %}"
method="POST">
{% csrf_token %}
<input type="hidden" name="current_post" value="{{ post.slug }}">
<input type="submit>
</form>
view:
def delete_post(request, comment_id=None):
Comment.objects.filter(id=comment_id).delete()
return redirect('forum:post_detail', slug= request.POST['current_post'])
It's not obvious if by "I want them to stay on the same post_detail page" you mean that you dont want to perform the redirect in browser and you'd rather delete it via ajax. If that's the case, please check the Django documentation about AJAX request with CSRF, as there's an example how to do exactly that.
I am looking to change the wording of a button depending on a url path. I have tried to use some {% if blocks %} but unable to get it to work.
I have tried to the following if block
<div class="form-group">
{% if url == 'http://localhost:8000/client/<int:pk>/update/' %}
<button class="btn btn-outline-info" type="submit">Update</button>
{% else %}
<button class="btn btn-outline-info" type="submit">Post</button>
{% endif %}
</div>
This is my url in my apps urls.py
path('client/<int:pk>/update/', PostUpdateView.as_view(), name="post-update"),
Any help would be handy.
You can store the url in a temporary variable and then use it against if condition.
In django template:
{% url 'post-update' pk=1 as the_url %}
{% if request.path == the_url %}
<button class="btn btn-outline-info" type="submit">Update</button>
{% else %}
<button class="btn btn-outline-info" type="submit">Post</button>
{% endif %}
Here request.path is your current url.
I want to style my checkboxes to look like buttons using the bootstrap class <div class="btn-group-toggle" data-toggle="buttons">
but it does not let me retrieve checkbox values in Flask like it usually did when I did not use this bootstrap class.
I have created a page with dynamically generated checkboxes using the code below. I use the following Flask code to retrieve checkbox values checkboxValues = request.form.getlist('checkbox'). How can I retrieve these checkbox values whilst having it nicely styled as buttons?
<form action="start" method="post">
{% for text in slideText %}
{% set count = namespace(a=0) %}
{% set slideNumber = namespace(a=loop.index-1) %}
<h2>Slide {{ loop.index }}</h2>
<hr>
<div class="btn-group-toggle" data-toggle="buttons">
{% for innerText in text %}
<label class="btn btn-primary active">
<input type="checkbox" name="checkbox" autocomplete="off" value="{{ slideNumber.a }}.{{ count.a }}"> {{ innerText }}
</label>
{% set count.a = count.a + 1 %}
{% endfor %}
</div>
{% endfor %}
<br>
<input class="btn btn-block btn-outline-success" type="submit" value="Submit">
</form>
So I'm rendering a template, including a table...and for one of the columns, the value is blank or contains a Delete button/form (depending on whether delete is possible).
Anyway, my issue is that if the Delete button is present the row height is twice that of the others, making my table look rather ugly.
{% if condition %}
<td>
<form name="input" action="./delete/" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="submit" name={{item}} value="Delete">
<input type="hidden" name="filter_start" value={% if date1 %}{{date1}}{% endif %}>
<input type="hidden" name="filter_end" value={% if date2 %}{{date2}}{% endif %}>
</form>
</td>
{% else %}
{% endif %}
Is there anything clearly wrong with the above? The "hidden" bits are so that I can retrieve certain 'dates' with the 'delete' button.
Try to add style="display:inline;" on the form.