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 ;)
Related
When def login() function call and redirect to def index() function my url change in the browser and look like http://127.0.0.1:8000/error500index this url. But logically url look like http://127.0.0.1:8000/index this. But error500 show in the url when i use redirect function, error500 is my last url in the Project urls.py and APP urls.py.
Anyone help me out what is the happening?
view.py
from django.shortcuts import render, redirect
from django.contrib import messages
from django.http import HttpResponse
from .models import WebUser
def index(request):
return render(request, 'index.html')
def login(request):
if (request.method == 'POST'):
login_email = request.POST['email']
login_password = request.POST['password']
# Compare with Database where input email exist!
try:
CheckUser = WebUser.objects.get(email=login_email)
except:
return HttpResponse("User Dosen't Exist!")
if (login_email == CheckUser.email and login_password == CheckUser.Password):
#When redirect function call my url change and pick the last url from project urls.py and this url appears in the browser http://127.0.0.1:8000/error500index
return redirect(index)
else:
return HttpResponse("Email or Password are wrong!")
else:
return render(request, 'login.html')
Project Urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('SIS_APP.urls')),
path('index', include('SIS_APP.urls')),
path('login', include('SIS_APP.urls')),
path('register', include('SIS_APP.urls')),
path('settings', include('SIS_APP.urls')),
path('weather', include('SIS_APP.urls')),
path('error404', include('SIS_APP.urls')),
path('error500', include('SIS_APP.urls')),
]
APP urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('index', views.index, name='index'),
path('login', views.login, name='login'),
path('register', views.register, name='register'),
path('settings', views.settings, name='settings'),
path('weather', weatherAPI.weather, name='weather'),
path('error404', views.error404, name='error404'),
path('error500', views.error500, name='error500'),
]
You are missing the slashes "/" in your urls. URLs are concatenated, so if you have, i.e,
path('index', views.index, name='index'), ## this will give ..indexsome-sub-route
path('index/', views.index, name='index'), ## this will give ..index/some-sub-route
i think you are not defining your urls correctly . from django docs django docs
you will see that redirect can be used with a hardcoded link that is
redirect(/index/)
and since you didnt add the slash in the url we have
redirect(index)
so it will pass the the value index to your url.
to fix it add / to ur url defination
Hail Devs. I have an app that in the index I call some queries with ForeignKey between table fields, using the User as argument. The views work for other tables without ForeignKey. But when I invoke the CRUD (delete, update, create) functions that request the database, it returns an error:
Request Method: GET
Request URL: http://127.0.0.1:8000/delete/13/
Django Version: 3.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'projeto' with no arguments not found. 1 pattern(s) tried: ['delete/(?P<pk>[0-9]+)/projeto/$']
Exception Location: C:\webcq\venv\lib\site-packages\django\urls\resolvers.py, line 694, in _reverse_with_prefix
Python Executable: C:\webcq\venv\Scripts\python.exe
Python Version: 3.8.6
Python Path:
['C:\\webcq',
'C:\\webcq\\venv',
'C:\\webcq\\venv\\lib\\site-packages']
Server time: Thu, 20 May 2021 21:03:07 +0000
url.py project
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('imagem.urls', namespace='home')),
path('view/<int:pk>/', include('imagem.urls', namespace='view')),
path('edit/<int:pk>/', include('imagem.urls', namespace='edit')),
path('update/<int:pk>/', include('imagem.urls', namespace='update')),
path('delete/<int:pk>/', include('imagem.urls', namespace='delete')),
]
urls.py app
from django.urls import path
from . import views
app_name = 'imagem'
urlpatterns = [
path('', views.home, name='home'),
path('projeto/', views.projeto, name='projeto'),
path('form/', views.form, name='form'),
path('create/', views.create, name='create'),
path('view/<int:pk>/', views.view, name='view'),
path('edit/<int:pk>/', views.edit, name='edit'),
path('update/<int:pk>/', views.update, name='update'),
path('delete/<int:pk>/', views.delete, name='delete'),
]
views.py app
The views work, but do not redirect to the view name and return the above error.
def edit(request, pk):
data = {}
data['db'] = Projeto.objects.get(pk=pk)
data['form'] = ProjetoForm(instance=data['db'])
return render(request, 'imagem/form.html', data)
def update(request, pk):
data = {}
data['db'] = Projeto.objects.get(pk=pk)
form = ProjetoForm(request.POST or None, instance=data['db'])
if form.is_valid():
form.save()
return redirect('imagem:projeto')
def delete(request, pk):
db = Projeto.objects.get(pk=pk)
db.delete()
return redirect('imagem:projeto')
form.html
{% for dbs in db %}
<td>
<th>{{dbs.id}}</th>
<td>{{dbs.nome}}</td>
<td>{{dbs.tipo_img}}</td>
<td>
Visualizar
Editar
Deletar
</td>
</tr>
{% endfor %}
models.py app
class Projeto(models.Model):
tipo_img = models.ForeignKey(TipoImg, on_delete=models.CASCADE)
nome = models.CharField(max_length=200)
autor = models.ForeignKey(User, on_delete=models.CASCADE, default='1')
def __str__(self):
return self.nome
Since the projeto is in the imagem urls, which is called from urls.py with a namespace of 'home', I belive you want to use return redirect('home:projeto') in your views (not 'imagem:projeto').
You probably also want to get rid of the 'view', 'edit', 'update', and 'delete' from urls since you have it in imagem as well (or remove it from imagem).
Your first url.py should probably be:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('imagem.urls', namespace='home')),
]
For your future self, you probably want to name your root url file as urls.py; other urls.py will live in their own app folders usually.
So a file structure like this would result:
project_name/project_name/settings.py
project_name/project_name/urls.py
project_name/imagem/urls.py
project_name/imagem/views.py
project_name/imagem/models.py
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.
I am using Django version 1.10.7. Currently in my html I have:
</span>
My project urls.py has
from django.conf.urls import include,url
from django.contrib import admin
from base import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^team/', include('team.urls'), name='team'),
url(r'^game/', include('game.urls'), name='game'),
url(r'^about/', include('base.urls'), name='about'),
url(r'^$', views.home, name='home'),
]
And in views.py,
from django.shortcuts import render
# Create your views here.
def about(request):
return render(request,'base/about.html',{})
def home(request):
return render(request,'base/home.html',{})
This gives the error:
NoReverseMatch at /
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.10.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How can I fix this?
Try to put your urls in this order:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
url(r'^team/', include('team.urls'), name='team'),
url(r'^game/', include('game.urls'), name='game'),
url(r'^about/', include('base.urls'), name='about'),
]
if works, it means that there is a problem in your other included urls files.
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.