Django URL Page error when link is clicked twice - django

I have a url defined as:
path("home/projects/",project_view,name = "viewProject")
This render correctly when i click on the 'Details' link on project_view.html
<li>
<a href="projects" data-toggle="tooltip" class="tip-bottom" title="All Projects">
<i class="fa fa-list" aria-hidden="true">Details</i></a>
</li>
But while i am on the same page,if i click the same link again i get an error:
home/projects/ [name='viewProject']
"Page not found...The current path, home/projects/projects, didn't match any of these"
I understand what the error means,but how can i redirect the page to "home/projects/" if the link is clicked twice?

Instead of writing the url patterens you should use the django url template tag like this:
<a href="{% url 'viewProject' %}" data-toggle="tooltip" class="tip-bottom" title="All Projects">
<i class="fa fa-list" aria-hidden="true">Details</i></a>

Related

Django - button redirecting to {% url 'index' %}

I can't find any solution on any article so I'm asking here.
I'd like to make button which is gonna redirect user to specific url.
I have already did it this way:
<button onclick="location.href='create_recipe/'" type="button" >Create new Recipe</button>
but instead of passing whole link I'd like to use {% url 'some_view' %} but I do not have an idea how I should do that.
Is it even possible to do that ?
It has to be <button>,
edit:
something like:
<button type="button" class="btn btn-outline-secondary">Create new Recipe</button>
also does not work
You can do this by adding this to the button:
onclick="goToSomeView()"
And then add this in script tag of html file:
<script type="text/javascript">
function goToSomeView(){
document.location.href = "{% url 'some_view' %}"
}
</script>
index is your function which exist views.py file.
from django.urls import path
from . import views
path('',views.index, name='index'),
HTML:
<a class="btn btn-outline-secondary" href="{% url 'index' %}">Create new Recipe</a>
As I see you're using bootstrap already, so the easiest way is to use the a element with the type="button" attribute.
Create new recipe
Below you can see that the result is the same, but for button you need to bind the onclick function. In this case I pointed to #nigel239 answer.
function goToSomeView(){
document.location.href = "{% url 'index' %}"
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<p class="lead">Using 'a' element</p>
Create new recipe
<p class="lead mt-3">Using button</p>
<button onclick="goToSomeView()" class="btn btn-outline-secondary">Create new recipe</button>

How to get a button to link to another HTML template in Django?

I'm new to Django, I'm trying to get a button on the homepage to link to another (as of right now) static page.
I thought this was pretty simple, I've done frontend work before and a simple href to the file would be enough but for some reason its not linking.
<h1> This is the homepage yay!</h1>
<div class="container">
Judges
<button type="button" class="btn btn-2">Students</button>
</div>
If I understood the question well, you would have to create a view and put its url in the template.
def scoring_sheet(request):
return render(request, 'scoringsheet.html', {})
And register in your url:
path('yourapp/scoring_sheet', views.scoring_sheet, name='scoring-sheet'),
and add in html;
<h1> This is the homepage yay!</h1>
<div class="container">
Judges
<button type="button" class="btn btn-2">Students</button>
</div>

obtain django dropdown menu item queryset

I am trying to make a calendar html page, that has a dropdown button to select the different months. How to get to this calendar page is via the nav bar that is created at base.html
base.html - how to get to the calendar page.
....
....
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" data-target="scheduler_dropdown" href="#"><i class="fas fa-calendar"></i>Scheduler</a>
<div class="dropdown-menu" aria-labelledby="scheduler_dropdown">
<a class="dropdown-item" href="{% url 'view_schedule' %}"><i class="fas fa-calendar-alt"></i>View Schedule</a>
</div>
</li>
what i've build so far:
urls.py
urlpatterns = [
path('schedule/view-schedule/', views.view_schedule, name='view_schedule'),
path('schedule/view-schedule/?query=month<str:selected_month>', views.view_schedule,
name='view_schedule_selected_month'),
]
Views.py
def view_schedule(request, selected_month=None):
if request.method == 'POST':
print('post')
else:
current_month = date.today().month
current_year = date.today().year
# a = request.GET # How to get query set from dropdown menu???
# print(a)
args = {
'month_cal': monthcalendar(current_year, current_month),
'month_name': calendar.month_name[current_month],
'year_name': current_year,
}
return render(request, 'static/html/view_schedule.html', args)
view_schedule.html
<div class="card-header">
Schedule for {{ month_name }} {{ year_name }}
<form class="date-selector" method="post">
{% csrf_token %}
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="far fa-caret-square-down"></i>
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href={% url 'view_schedule_selected_month' selected_month=1 %}>Jan</a>
<a class="dropdown-item" href={% url 'view_schedule_selected_month' selected_month=2 %}>Feb</a>
<a class="dropdown-item" href={% url 'view_schedule_selected_month' selected_month=3 %}>Mar</a>
</div>
</div>
</form>
</div>
My problem is that, when I click on the drop down button and select the relevant month Jan, Feb, Mar, the url changes, but in my views.py, the query set doesn't appear. So I can't extract the query for processing.
Any thoughts?
Turns out I could have just done print(selected_month) and it would print the query result.. I got the idea when I was watching this video: https://www.youtube.com/watch?v=qmxoGYCFruM
Don't use urlpatterns to handle query strings. urlpatterns handles only the URL itself; query parameters are part of the GET data and are handled within the callback method. You'll need to change the way your HTML, urlpatterns, and the view work to accommodate this.
urlpatterns = [
path('schedule/view-schedule/', views.view_schedule, name='view_schedule'),
]
In your HTML, you'll want a form with a dropdown that GETs the data to the URL above. You can use the select tag for this.
And then in the view, you can extract GET data from request.GET. Specifically, if you used the select tag as suggested above, then the user's choice will be in request.GET[NAME] where NAME is the name of the select tag.
There are other ways to go about this, depending on aesthetic preferences, etc., but the method I've explained above is likely to be the easiest.
Also, query set (or QuerySet) has a very specific meaning in Django. It refers to a type of object used in database queries as explained here. The results of an HTML form are not "query sets."

How can I login again when I logout using Google Plus Auth

To login with Google Plus Auth, I use the example of Python Social Auth at https://github.com/omab/python-social-auth/tree/master/examples/django_example
And I have the scenario as follows:
Firstly, I login by google plus sign in button
<div id="signinButton">
<span class="g-signin" data-scope="{{ plus_scope }}"
data-clientid="{{ plus_id }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
Secondly, I click on the logout button to sign out
<a class="btn btn-primary" href="/logout/">
<i class="fa fa-sign-out"></i>
Logout
</a>
Finally, when I login again by Google Plus sign button, the Google popup can not display to enter the username and password. However, I can login again by the button of Django example
<a class="col-md-2 btn btn-default" name="{{ backend|backend_class }}" href="{% url "social:begin" backend=name %}">
<i class="fa fa-{{ name|icon_name }}"></i>
{{ backend|backend_name }}
</a>
But I don't like this method because this is not recommended from Google https://developers.google.com/+/web/signin/redirect-uri-flow
As I understand, Google popup cannot display because I logged in by Google. I just logout from my django example app. For that reason, the popup cannot open.
However, I think this is a common action of users. They can login/logout many times from our Django apps and they don't need to know about their Google status.
So, is there any approach to login again for my scenario?
Thank you so much
Notes: This is my screen shot

Passing an id in Django url

I want to pass userid from django url to my view
Here is what I have written in Django template
<a href ={% url 'user_details' x.id %} class='btn btn-primary' style="float: right;" >Know More</a></div>
To handle this url I have written Url like
url(r'^User/(\d{userid})/$', 'search.views.user_detail',name='user_details'),
But I am getting an error i.e
NoReverseMatch at /search/
Reverse for ''user_details'' with arguments '(2L,)' and keyword arguments '{}' not found.
Please help me out What might I am doing wrong here .
No quote ''
<a href ={% url user_details x.id %} class='btn btn-primary' style="float: right;" >
Know More
</a>
Another your url
url(r'^User/(?P<userid>\d+)/$', 'search.views.user_detail', name='user_details'),
Be carefull, after Django 1.5, use must use quotes. I came across this solution and tried it, got an error. I'm using Django 1.6 and you need the quotes:
<a href ={% url 'user_details' x.id %} class='btn btn-primary' style="float: right;" >
Know More
</a>
hope it helps.