django url doesn't redirect properly - python

I am trying to send user to next page using button like this
Weekly Report
Daily Report
but when I click on header or another button at daily URL keeps stacking like daily/weekly
instead /daily or /weekly. It doesn't clear URL and go to button instead just keeps adding it.
This is url.py
urlpatterns = [
path('', views.index, name="Home"),
path('weekly/', views.weekly, name="weekly"),
path('daily/', views.daily, name="daily"),
]
I tried different ways to call URL like {% url 'weekly' %} but still it's the same.
All urls only keep stacking. even home page button does keep stacking

You need to change to urls to either:
Weekly Report
Daily Report
Or use {% url %} tag in template:
Weekly Report
Daily Report

Related

Cant get URL,s in urls.py to work on DJANGO project

This is the urls.py file for the main site in my Django project.
Ive got a category table and a country table linked to a product table and I cant get all the urls in the code below working. If i put the category_detail url in the list just after the slug:category_slug>/slug:slug/', product_detail url those urls work and country_detail links dont. If i put the country_detail urls after the slug:category-slug>/slug:slug/', product_detail url then only the country_detail url works. Any help appreciated
from django.contrib import admin
from django.urls import path
from core.views import frontpage
from store.views import product_detail, category_detail,country_detail
urlpatterns = [
path('', frontpage, name='frontpage'),
path('admin/', admin.site.urls),
path('<slug:category_slug>/<slug:slug>/', product_detail, name='product_detail'),
path('<slug:slug>/', country_detail, name='country_detail'),
path('<slug:slug>/', category_detail, name='category_detail'),```
If i put the category_detail url in the list just after the slug:category_slug>/slug:slug/', product_detail url those urls work and country_detail links dont. If i put the country_detail urls after the slug:category-slug>/slug:slug/', product_detail url then only the country_detail url works.
This is because Django will use the first route that matches. Since the rules to match country detail and product detail are both exactly the same, whichever one is first is the one that will match.
To solve this, you need to rethink your API design and create routes that can be matched unambiguously.
Often we design a REST API around "resources" and "collections". This means we will have a route /products/ such as to interact with a collection and /products/1 to interact with a specific product resource.
For the collection route /products, we can add the ability to pass in query parameters to filter on a category or country with something like /products?country=United+States (note the + is the URL encoded version of a space). To allow this on the API side, you will need to get the query parameters from the request object and then add a filter to your queryset. I won't get into the details here, but you can find more information about this with some googling.
For the resource route products/1, we might want to get related objects, like the categories for that country. In such a case, we would create a route like products/1/categories. I will leave the details for implementing the view for this as an exercise for the reader.
Try this
urlpatterns = [
path('', frontpage, name='frontpage'),
path('admin/', admin.site.urls),
path('product-detail/<slug:category_slug>/<slug:slug>/', product_detail, name='product_detail'),
path('country-detail/<slug:slug>/', country_detail, name='country_detail'),
path('category-detail/<slug:slug>/', category_detail, name='category_detail'),
]

how to use google login link to my login page using django-allauth?

I have created a project which is working nicely.
I have a login page, signup page and profile page (as myspace). I just want to add an additional funtionality to my login page which is a user can login using google, fb, or instagram. but when i added the django-allauth to my project. it overwrite the existing login page.
I just want to add an icon of google login. and not the whole page how i can do so.
my login page looks like this earlier and at bottom i added a google icon in which i want to add the functionality of auth app
login pages is looking like this for now
Update:-
what i thought is how django-allauth works, that i just need to add a url in login page with <a href > tag with 'allauth/google' or something like this.
what i see is i need to add a new path in urls.py for allauth which automatically create a login, signup and profile page.
problem which i figure out is
I have an accounts_app app in my project. the url for accounts_app is same as allauth thats why it overwrite the template
url.py of camorid_project
urlpatterns = [
path('', include('camroid_app.urls')),
path('accounts_app/', include('accounts_app.urls')), -----i want to add allauth functionality to this url
path('accounts/', include('allauth.urls')), -------------i don't want to add this because it create an inbuilt templates
path('admin/', admin.site.urls),
]
what i want is i don't want to change login, signup and myspace page and i want to add icon in my login page with google login on click of which a user would be able login to the website.
in your login.html add {% load socialaccount %}
and then <a href="{% provider_login_url 'google' %}"</a>
this link will make you login with google api.
and you can style the button if you wnat..

Django 3 - NoReverseMatch

I'm somehow new to Django, and I don't know how to resolve what seems to be a simple bug. A lot of peoples asked more or less the same question here but any to their fixes worked for me.
So I have 2 apps, one that will work as the main menu for other apps:
Main Menu urls.py :
re_path(r'^elec/boq/', include('a0101_boq_elec_main.urls', namespace="SAMM")),
Then in the urls.py of that app I have this:
app_name='BoqElecMM'
urlpatterns = [
path('', views.boq_index.as_view(), name='boqIndex'),
path('search/', views.SearchResultView.as_view(), name='searchResult'),
path('<int:boq_project_id>', views.BoqProjectInfo.as_view(), name='BPInfo'),
]
But when I'm trying to use this in my template: <a href="{% url 'BoqElecMM:BPInfo' %}"> , I'm getting this Django error :
NoReverseMatch at /elec/boq/
Reverse for 'BPInfo' not found. 'BPInfo' is not a valid view function or pattern name.
Could some of you tell me please, what I'm doing wrong here?
Thank you in advance.
You need to pass a value for the boq_project_id parameter, so for example
{% url 'BoqElecMM:BPInfo' boq_project_id=42 %}
Of course the value you pass to the boq_project_id is normally the id of the project object. So for example, if in your template you have a project object, you can write:
{% url 'BoqElecMM:BPInfo' boq_project_id=project.pk %}
if you have an namespace=… as well, then this should also be prefixed, so with the data in your view, it is:
{% url 'SAMM:BoqElecMM:BPInfo' boq_project_id=project.pk %}

Getting 403 (Forbidden) when trying to send POST to set_language view in Django

I'm new to Django and am trying to create a small website where I click on a flag and the language changes. I'm using django i18n for that:
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [url(r'^i18n/', include('django.conf.urls.i18n'))]
urlpatterns += i18n_patterns(
url(r'^$', views.home, name='home'),
)
The problem is, when I run the following code:
templetatags.py
#register.simple_tag
def test():
r = requests.post('http://localhost:8000/i18n/setlang/', data = {'lang':'en', 'next' : '/'})
print r.status_code
home.html
<div id='country_flags'>
<a hreflang="en" href="{% test %}"><img id='en' src='{% static "mysyte/images/gb.png" %}'></a>
</div>
the result of r.status_code is 403.
What am I doing wrong?
Any POST request in django requires you to send the CSRF-Cookie by default. Your options around this:
Don't use a POST request. Use GET instead.
Send the CSRF-Token with the request. https://docs.djangoproject.com/en/1.9/ref/csrf/
use the decorator #csrf_exempt to remove any csrf-protection from a view (in your case the view for http://localhost:8000/i18n/setlang/)
don't send a request to your own app. use a link for the user to click on. probably your best option.
Note:
if you want to use the decorator w/ a class-based view, decorate the dispatch() function of that view

Using Django templates in an iframe, 404 error

I have a Django site set up with the home page being home.html. Within home.html is a modal that is set to open automatically a few seconds after the home page loads. Within the modal is an iframe that should display template1.html. However when the modal comes up I have a 404 and I'm told the page didn't match the url patterns I defined. I'm not entirely sure where or how I go about getting template1.html to display in the iframe. The user isn't clicking on the main page to bring up a new template, which is how I'm familiar with using Django. Do I need to create another url pattern or view to achieve this or am I way off? Here are my files:
urls.py:
from django.conf.urls.defaults import patterns, url
from disease import views
urlpatterns = patterns('',
url(r'^$', views.home, name='disease_home'),
url(r'^disease/(?P<pk>\d+)/$', 'disease.views.disease', name='disease_disease'),
url(r'^treatment/(?P<pk>\d+)$', views.treatment, name='disease_treatment'),
home.html modal:
<div id="challengeModal" class="reveal-modal xlarge">
< iframe seamless scrolling="no" src="challenge1.html" name="challenge1"
id="challenge1"></iframe>
< /div>
views.py:
def home(request):
return render(request, "disease/home.html")`
Other views return the second template, template2.html, based on actions in template1.html
Thanks!
The short answer is Yes, you'll need both another url pattern AND a view for your iframe. An iframe is pretty much treated like an embedded browser window, it will make its own request to the server.
iframe src is just a url to a resource.
For example you could load your treatment page into it using
<iframe src="/treatment/1/"></iframe>
You will need to create a view to serve your challenge1.html template and an url to route to that view

Categories