Django somehow tries to render the wrong template - python

I am working on a project using Python 3.6 and Django 2.0.5. And I have a template named register_client.html that I would like to use to render a simple page that will register new clients to the Client model.
The following is my complete urlpatterns:
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^logout/$', views.user_logout, name='logout'),
re_path(r'^$', views.home, name='home'),
re_path(r'^home/$', views.home, name='home'),
re_path(r'^about/$', views.about, name='about'),
re_path(r'^blog/$', views.blog, name='blog'),
re_path(r'^contact/$', views.contact, name='contact'),
re_path(r'^lawsuits/$', views.lawsuits_list, name='lawsuits'),
re_path(r'^my_profile/$', views.my_profile, name='my_profile'),
re_path(r'^register_client/$', views.register_client, name='register_client'),
re_path(r'^client/(\d+)/$', views.client, name='client'),
re_path(r'^client/(\d+)/lawsuits/$', views.lawsuits_list, name='lawsuits'),
re_path(r'^client/(\d+)/edit/$', views.edit_client, name='edit_client'),
re_path(r'^client/(\d+)/remove/$', views.remove_client, name='remove_client'),
re_path(r'^lawsuit/(\d+)/$', views.lawsuit, name='lawsuit'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The pattern that should catch a request to http://localhost/register_client should be re_path(r'^register_client/$', views.register_client, name='register_client').
And here's the view that should render the template appropriately:
def register_client(request):
if request.user.is_authenticated and request.user.is_staff:
if request.method == "POST":
form = ClientForm(request.POST, request.FILES)
if form.is_valid():
# register a new client
...
else:
print(form.errors)
form = ClientForm()
return render(request, 'register_client.html', {'form': form})
return HttpResponseRedirect('/home/') # not_permitted view
With the above configurations, Django's giving me a NoReverseMatch at /register_client/. And it further states that it is looking up for the view with the name client, which is an entirely different view in my views.py - Reverse for 'client' with arguments '('',)' not found. 1 pattern(s) tried: ['client/(\\d+)/$']
I am running out of ideas and places to look for as to what could be the cause of this. Any help or tip will be highly appreciated, thanks in advance.

Related

Django: Page not found but path is shown

I have recently started using Django and I am a little lost as to why my services.html page does not render as I have followed what appears to be the same steps I took for my home page to work but I keep getting the error that nothing matches the current path. Below are my snippets for views and urls.py. I also had it be included in my website urls.py by using path('', include('affiliate.urls')),
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
[name='services']
The current path, services.html, didn't match any of these.
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.services, name="services"),
]
Views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html', {})
def services(request):
return render(request, 'services.html', {})
Change your urlpatterns as follows:
urlpatterns = [
path('', views.home, name="home"),
path('services/', views.services, name="services"),
]
Now when you want to access the services view, make sure to use 127.0.0.1:8000/services
because both of your paths for home and services are pointing to the same url.
Willem is trying to explain you the same thing.

Django views rendering just one page and wont render the rest

So im using django 1.8 on macosx and i have problem while configuring html, namely when i try to load another page except one that is set at default one(index is the default one), it just refreshes the default one i have set in urls.py and i cant access any other page except that one but in the url bar i can see that im accesing the proper html file because it says so but the page is not changing....heres my code:
app/urls.py-----------
urlpatterns = [
url(r'^contact/', views.contact, name='contact'),
url(r'^projects/', views.projects, name='projects'),
url(r'^services/', views.services, name='services'),
url(r'^', views.index, name='index'),
url(r'^about/', views.about, name='about'),
these are all the pages im trying to install
main urls.py-------------
from app import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^',include('app.urls')),
]
and this is my views.py-----------
def contact(request):
return render(request, 'app/template/contact.html',{})
def about(request):
return render(request, 'app/template/about.html',{})
def projects(request):
return render(request, 'app/template/projects.html',{})
def services(request):
return render(request, 'app/template/services.html',{})
def index(request):
return render(request, "app/template/index.html",{})
https://docs.djangoproject.com/en/1.8/intro/tutorial03/
you need $ to end the string. In your case he link all thats starts with all.
url(r'^$', views.index, name='index'),
you views.py:
def contact(request):
return render(request, 'app/contact.html',{})
def about(request):
return render(request, 'app/about.html',{})
def projects(request):
return render(request, 'app/projects.html',{})
def services(request):
return render(request, 'app/services.html',{})
def index(request):
return render(request, "app/index.html",{})

Django: return render not looking for urls.py in sub application

I have a urls.py file in mysite, which points to everything in the shark/ folder to the shark/urls.py file:
mysite/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include ('polls.urls')),
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'^shark/', include('shark.urls')),
url(r'^', include('polls.urls')),
In the shark/ folder, I have the following view file, with a function that loads target.html. However it cannot find this file, because it looks in the mysite/urls.py file instead the one in the shark/ folder:
def get_name(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 = NameForm(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(target)
def target(request):
if request.method == 'POST':
form = NameForm(request.POST)
answers=[]
if form.is_valid():
return render(request, 'target.html', {'form':form})
else:
return render(request, 'target.html', {'form': form})
And as a result I get this error message:
^admin/
^polls/
^accounts/
^shark/
^ ^$ [name='index']
^ ^admin/
The current URL, target/, didn't match any of these.
Even though the URL would be listed, but in the shark/urls.py file.
shark/urls.py
urlpatterns=[
url(r'^$',views.index, name='index'),
url(r'^get_name', views.get_name, name='get_name'),
url(r'^target', views.target, name='target'),
How can I correct this problem, so that render requests in the shark/ subfolder will reference the urls.py file in that particular folder and not the main urls.py in the mysite/ folder?
It would be a match if the url were: shark/target since you specified the shark prefix in the root urls.py. Use an empty string there and it will match target/. e.g.:
mysite\urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^polls/', include ('polls.urls')),
url(r'^accounts/', include('registration.backends.hmac.urls')),
url(r'', include('shark.urls')), # Now shark urls will match without the prefix
url(r'^', include('polls.urls')),
It's generally a good practice to keep app urls separate and include from a root urls module.
The shark/urls.py is only loaded for paths that begin with shark/, because that's what you are specifying in your mysite/urls.py. Therefore, to additionally load the path target/ from your "sharks" app, you have to specifically mention it in the main mysite/urls.py.
mysite/urls.py
urlpatterns = [
...
url(r'^shark/', include('shark.urls')),
url(r'^target/', shark.views.target)),
...

Django - NoReverseMatch error in a simple form

i am stuck with this error:
"Reverse for 'create' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$create$']"
I can't see where the problem is.
The log is an app in my general project
Here is my Project/log/templates/log/index.html that contain my form:
<form action="{% url 'log:create' %}" method="post">
//...
</form>
Here is the Project/Project/urls/py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('log.urls', namespace='log')),
url(r'^log/', include('log.urls', namespace='log')),
url(r'^admin/', include(admin.site.urls)),
]
Here is my Project/log/urls.py :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', views.logout, name='logout'),
url(r'^create$', views.create, name='create'),
]
Then I defined my function create in the Project/log/views.py :
def create(request):
if request.method == "POST" and request.POST['name'] and request.POST['password']:
name_p = escape(request.POST['name'])
pass_p = escape(request.POST['password'])
try:
login = User.objects.get(username=name_p)
except:
user = User.objects.create_user(name_p, name_p+'#email.com', pass_p)
user.save()
return HttpResponseRedirect('log:index')
else:
return render(request, 'log/index.html', {'error_message' : 'The login you chose already exists',})
else:
return render(request, 'log/index.html', {'error_message' : 'You did\'t fill the entire form',})
I don't know where is my mistake because the form is a simple one with no arguments passed and the regular expression in the url is a simple one too. It must be a problem in my function I think. If someone could lead me to the right path to fix this kind of error that would be great ;)

django namespace NoReverseMatch

I can't figure out what I've done wrong here. I'm using Django 1.6.5 with Python 2.7.6 on Ubuntu 14.04. I have a project called research containing an app called notecards. Here is my research/urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'research.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^notecards/', include('notecards.urls', namespace='notecards')),
)
And here is the call to it in notecards/views.py
def new_note(request):
if request.method == 'GET':
form = NotecardForm()
else:
form = NotecardForm(request.POST)
if form.is_valid():
content = form.cleaned_data['content']
source = form.cleaned_data['source']
page = form.cleaned_data['page']
tags = form.cleaned_data['tags']
return HttpResponseRedirect(reverse('notecards:index', ()))
return render(request, 'notecards/new_note.html', {'form': form,
})
And here is the error I'm getting: u'notecards' is not a registered namespace. I must be missing something obvious, and yet I can't figure it out.
In your code, change the reverse('notecards:index', ()) to reverse('notecards:index'). The signature of reverse is
reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None)
You could see that urlconf mistakenly takes () instead of args taking it.
Moreover, have a look at the django.shortcuts.resolve_url, it's easier for normal use.

Categories

Resources