I'm working on a bilingual website French and English, and I'm trying to figure out how to detect and redirect the French users to index_fr.html (in templates folder).
My urls.py (it only shows the english ver. for now):
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', profiles.views.home, name='home'),
]
Thanks for your responses.
As dhke said, I enabled LocalMiddleware and used request.LANGUAGE_CODE to write a function in views.py that renders a template depending on the language and it works!:
views.py:
from django.http import HttpResponse
def home(request):
context={}
if request.LANGUAGE_CODE == 'fr':
template='index_fr.html'
else:
template='index_en.html'
return render(request,template,context)
def fr(request):
context={}
template='index_fr.html'
return render(request,template,context)
def all(request):
context={}
template='index_en.html'
return render(request,template,context)
urls.py:
urlpatterns =[
url(r'^admin/', admin.site.urls),
url(r'^$', profiles.views.home, name='home'),
url(r'^index_fr.html', profiles.views.fr, name='fr'),
url(r'^index_en.html', profiles.views.all, name='all'),
]
Related
I am trying to render to different page using URLs in django,I have written all codes as the tutorials as well,
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name="index"),
path('', views.register,name="register"),
path('', views.login,name="login")
]
def index(request):
print("hello index")
return render(request,'index.html')
def register(request):
return render(request,'register.html')
def login(request):
print("hello login")
return render(request,'login.html')
but, whenever I hit that register link on my HTML it just always takes me to index.html.
<ul class="nav">
<li> Home</li>
<li> Register</li>
<li> Login</li>
</ul>
I have also tried putting something in the routes
urlpatterns = [
path('admin/', admin.site.urls),
path('index', views.index,name="index"),
path('register', views.register,name="register"),
path('login', views.login,name="login")
]
but this time it gives errors like The empty path didn’t match any of these.
I have worked on different django project using the above pattern and it was working perfectly fine.Where am I making it wrong? Any help would greatly be appreciated!
you have same path name so you need to give different path for different views.
for Example:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index,name="index"),
path('registe/', views.register,name="register"),
path('login/', views.login,name="login")
]
def index(request):
print("hello index")
return render(request,'index.html')
def register(request):
return render(request,'register.html')
def login(request):
print("hello login")
return render(request,'login.html')
I am having trouble with my urls. I have one app called users that has two models, Salon and Stylist. The url /stylists and /salons is rendering the same view (stylist_results) however /salons should render salon_results and I cannot figure out why. I think there may be something wrong with the way I am using namespaces.
users/urls.py
from django.conf.urls import url
#import views from the current directory
from . import views
urlpatterns=[
url(r'^$', views.stylist_results, name='stylist_results'),
url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
url(r'^$', views.salon_results, name='salon_results'),
url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]
users/views.py
from django.shortcuts import get_object_or_404, render
from .models import Salon, Stylist
# Create your views here.
def salon_results(request):
salons = Salon.objects.all()
return render(request, 'salons/salon_results.html', {'salons': salons})
def salon_detail(request, pk):
salon = get_object_or_404(Salon, pk=pk)
return render(request, 'salons/salon_detail.html', {'salon': salon})
def stylist_results(request):
stylists = Stylist.objects.all()
return render(request, 'stylists/stylist_results.html', {'stylists': stylists})
def stylist_detail(request, pk):
stylist = get_object_or_404(Stylist, pk=pk)
return render(request, 'stylists/stylist_detail.html', {'stylist': stylist})
urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from . import views
urlpatterns = [
url(r'^salons/', include('users.urls', namespace='salons')),
url(r'^stylists/', include('users.urls', namespace='stylists')),
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
]
urlpatterns += staticfiles_urlpatterns()
views.py
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Split your stylist and salon views into two separate modules. You might consider creating two different apps, but you don't have to.
users/stylist_urls.py
urlpatterns = [
url(r'^$', views.stylist_results, name='stylist_results'),
url(r'^(?P<pk>\d+)$', views.stylist_detail, name='stylist_detail'),
]
users/salon_urls.py
urlpatterns = [
url(r'^$', views.salon_results, name='salon_results'),
url(r'^(?P<pk>\d+)$', views.salon_detail, name='salon_detail'),
]
Then update your project's urls.py with the new modules:
urlpatterns = [
url(r'^salons/', include('users.salon_urls', namespace='salons')),
url(r'^stylists/', include('users.stylist_urls', namespace='stylists')),
...
]
At the moment, the regexes for your salon URLs are exactly the same as the stylist URLs, so the salon URLs will always match first.
You are specifying same set of url's(users.urls) for salons and stylists here:
url(r'^salons/', include('users.urls', namespace='salons')),
url(r'^stylists/', include('users.urls', namespace='stylists')),
What did you expect to happen
You are including same users/urls.py in urls.py
It does following:
find me /stylist/ => go into included urls
find first occurance of url(r'^$', views.stylist_results, name='stylist_results'),
renders that view
same thing happens with /salons/
URL Dispatcher documentation
I'm just finding my way around python/django. I'm in the middle of trouble shooting another issue with assistance and in the process of trying various things am now seeing this error in my console:
__import__(name)
File "/Users/vaijoshi/PycharmProjects/adec/src/project/urls.py", line 12, in <module>
url(r'^termsandconditions/$', terms_and_conditions, name='terms_and_conditions'),
NameError: name 'terms_and_conditions' is not defined
I'm a little confused as this information is defined in my views.py (at least that's what I think I'm doing)
views.py
from django.http import HttpResponseRedirect
from django.shortcuts import render
# Create your views here.
from src.adec.forms import UserForm
def home(request):
return render(request, "home.html")
def register_professional(request):
return render(request, "registerprofessional.html")
def register_user(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = UserForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form
else:
form = UserForm()
return render(request, 'registeruser.html', {'form': form})
def terms_and_conditions(request):
return render(request, "termsandconditions.html")
def how_it_works(request):
return render(request, "howitworks.html")
def search_results(request):
return render(request, "searchresults.html")
def profile(request):
return render(request, "profile.html")
URL.py in the project folder (note I was advised to move the above views.py from the projects folder to the src folder for the other issue I'm troubleshooting. Do they need to be in the same folder?)
import...
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/docs/', include('django.contrib.admindocs.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^termsandconditions/$', terms_and_conditions, name='terms_and_conditions'),
url(r'^how-it-works/$', how_it_works, name='how_it_works'),
url(r'^$', home, name='home'),
url(r'^registerprofessional/$', register_professional, name='register_professional'),
url(r'^registeruser/$', register_user, name='register_user'),
url(r'^searchresults/$', search_results, name='search_results'),
url(r'^profile/$', profile, name='profile'),
]
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My tree structure atm:
Kindly assist
Update based on comments below:
I have now done:
from adec.views import terms_and_conditions
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/docs/', include('django.contrib.admindocs.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^termsandconditions/$', terms_and_conditions, name='terms_and_conditions'),
url(r'^how-it-works/$', how_it_works, name='how_it_works'),
url(r'^$', home, name='home'),
url(r'^registerprofessional/$', register_professional, name='register_professional'),
url(r'^registeruser/$', register_user, name='register_user'),
url(r'^searchresults/$', search_results, name='search_results'),
url(r'^profile/$', profile, name='profile'),
]
if settings.DEBUG:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This is reverting me back to the original error I was facing:
__import__(name)
File "/Users/vaijoshi/PycharmProjects/adec/src/project/urls.py", line 1, in <module>
from adec.views import terms_and_conditions
File "/Users/vaijoshi/PycharmProjects/adec/src/adec/views.py", line 5, in <module>
from src.adec.forms import UserForm
ImportError: No module named src.adec.forms
Make sure that the name is imported in the urls.py:
from adec.views import terms_and_conditions
Alternatively, use string instead:
url(r'^termsandconditions/$', 'adec.views.terms_and_conditions',
name='terms_and_conditions'),
I am new in Python, I have a problem with the admin URL.
When I type
localhost:8000/admin/
My browser tells me:
DoesNotExist at /admin/
Question matching query does not exist
My index page and detail pages work fine.
I thought it was a mistake in the order of URL's,
I changed the order of the admin and detail URL,
but still nothing.
Can somebody please give me a hint.
project/urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'books.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<book_title>[\w_-]+)/$', 'books.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
books/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all
context = {'latest_question_list': latest_question_list}
return render(request, 'books/index.html', context)
def detail(request, book_title):
question = Question.objects.get(title=book_title)
return render(request, 'books/detail.html', {'question': question})
I forgot to put parentheses for Question.objects.all So silly of me.
add this before urlpatterns in urls.py
admin.autodiscover()
I tried to go 127.0.0.1:8000/poll/ it doesn't open, but I can go to /poll/20/result.
I just add other urls of poll and base url-> /poll/ stop to be opened.
Before I add to detail,result,vote function/url there were no problem to open index of poll.
Here are my codes:
In view.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You are at the poll index.")
def detail(request, poll_id):
return HttpResponse("You are looking at poll %s" %poll_id)
def results(request, poll_id):
return HttpResponse("You are looing at the results of poll %s" % poll_id)
def vote(request, poll_id):
return HttpResponse("You are voting on poll %s" %poll_id)
In Polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns( '',
url(r'^Ş', views.index, name='index'),
url(r'^(?P<poll_id>\d+)/$', views.detail, name='detail'),
url(r'^(?P<poll_id>\d+)/results/$', views.results,name='results'),
url(r'^(?P<poll_id>\d+)/vote/$', views.vote, name='vote'),
)
In mysite/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'sign_ups.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
In your MySite.urls,
you have mentioned the polls url as polls and given the url as poll.
Change any one of those to get the app working.