After getting post data from the following form, the page should redirect to 'associate:learn' as shown in the action. However, it just stays on the radio button page. I suspect I'm making a beginner's error but after rereading the tutorial, I'm not sure what's going on.
index.html
Choose a dataset
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'associate:learn' %}" method="post">
{% csrf_token %}
{% for dataset in datasets %}
<input type="radio" name="dataset" id="dataset{{ forloop.counter }}" value="{{ dataset.id }}" />
<label for="dataset{{ forloop.counter }}">{{ dataset }}</label><br />
{% endfor %}
<input type="submit" value="learn" />
</form>
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^images/', include('images_app.urls', namespace="images_app")),
url(r'^associate/', include('associate.urls', namespace="associate")),
url(r'^admin/', include(admin.site.urls)),
)
views.py
def index(request):
images = Image.objects.all()
datasets = []
for i in images:
if i.rank() >= 3:
datasets.append(i)
return render(request, 'associate/index.html', {'datasets':datasets})
The original HTML should redirect to this page.
learn.html
THIS IS THE LEARN PAGE
Can you go to associate:learn directly?
In your first urls.py
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^$', "associate.views.learn", name='learn'),
)
The url will always match "associate.views.index" since it appears before "associate.views.learn" and they both have the same url.
You should change it to something like:
urlpatterns = patterns('',
url(r'^$', "associate.views.index", name='index'),
url(r'^learn_or_something$', "associate.views.learn", name='learn'),
)
Hope this helps.
Your associate "index" and "learn" views both have the same URL. You need to have some way of distinguishing between them, otherwise the URL will always be served by the first one, which is index.
Related
I'm working on my project for a course and I'm totally stuck right now.
Login doesn't work. I using class-based views. When I`m logged in, everything works.The Logout View works too.
The error is as follows:
NoReverseMatch at /
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 3.1.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
Exception Location: /home/aleo/#HOME/.virtualenvs/znajdki/lib/python3.8/site-packages/django/urls/resolvers.py, line 685, in _reverse_with_prefix
Python Executable: /home/aleo/#HOME/.virtualenvs/znajdki/bin/python3
Python Version: 3.8.6
My files:
poszukiwania/urls.py
from django.urls import path
from . import views
from django.contrib.auth import views as auth_views
app_name = 'poszukiwania'
urlpatterns=[
path('<slug:kategoria_slug>/', views.rzeczy_list, name='rzeczy_list_by_category'),
path('<int:year>/<int:month>/<int:day>/<slug:rzecz_slug>/<int:id>', views.rzecz_detail, name='rzecz_detail'),
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('', views.rzeczy_list, name='rzeczy_list'),
]
znajdki/urls.py
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('poszukiwania.urls', namespace='poszukiwania'))
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL)
settings.py
LOGIN_REDIRECT_URL = 'poszukiwania:rzeczy_list'
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
templates/registration/login.html
{% extends "poszukiwania/base.html" %}
{% load static %}
{% block title %}Logowanie{% endblock %}
{% block content %}
<h1>Logowanie</h1>
{% if form.errors %}
<p>
Nazwa użytkownika lub hasło są nieprawidłowe.
Spróbuj ponownie
</p>
{% else %}
<p>Wypełnij aby się zalogować</p>
{% endif %}
<div class="login-form">
<form action="{% url 'poszukiwania:login' %}" method="post">
{{ form.as_p }}
{% csrf_token %}
<input type="hidden" name="next" value="{{ next }}" />
<p><input type="submit" value="Zaloguj się"></p>
</form>
</div>
{% endblock %}
help me, please
I configured the path and made the template but when I try to run server it gives
NoReverseMatch at /password-reset/
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
Request Method: POST
Request URL: http://localhost:8000/password-reset/
Django Version: 3.0.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
error.
My urls.py
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from users import views as user_views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('register/', user_views.register, name= 'register'),
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'users/logout.html'), name='logout'),
path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),
name='password_reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),
name='password_reset_done '),
path('password-reset-confirm///',
auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),
name='password_reset_confirm '),
path('', include('blog.urls'))
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and my template
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class = "content section">
<form method="POST">
{% csrf_token %}
<fieldset class= "form-group">
<legend class = "border-bottom mb-4">Reset Password</legend>
{{form|crispy}}
</fieldset>
<div class = "form-group">
<button class="btn btn-outline-info" type ="submit"> Reset Password </button>
</div>
</form>
</div>
{% endblock content %}
Could you please tell me what is wrong with my project? Thank you
It looks like you got a space in the name of 'password_reset_confirm '
'password_reset_confirm '
^
Not sure how django well django handles that. If that is not the issue, please post the code where you reverse-call the URL in the template (where the {% url ... %} template tag is or possibly from your view where you configured your e.g. success URL.
I am fairly new to Django and I am totally stuck on what is causing this error. I have done lots of searching but to no avail! Any help would be super appreciated.
The actual form works fine but when I try and submit the input data I get the error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^$ [name='home']
^patientlist [name='patient_list']
^patientdetail/(?P<pk>\d+)/$ [name='patient_detail']
^add_patient/$ [name='add_patient']
The current URL, spirit3/add_patient/, didn't match any of these.
My urls.py in the mysite directory looks like:
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('spirit3.urls')),
]
My urls.py in the app looks like:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^patientlist', views.patient_list, name='patient_list'),
url(r'^patientdetail/(?P<pk>\d+)/$', views.patient_detail, name='patient_detail'),
url(r'^add_patient/$', views.add_patient, name='add_patient'),
]
The relevant part of views.py:
def add_patient(request):
if request.method == 'POST':
form = PatientForm(request.POST)
if form.is_valid():
form.save(commit=True)
return redirect('home')
else:
print form.errors
else:
form = PatientForm()
return render(request, 'spirit3/add_patient.html', {'form':form})
And the html looks like:
{% extends 'spirit3/base.html' %}
{% block content %}
<body>
<h1> Add a Patient </h>
<form action="/spirit3/add_patient/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create Patient" />
</form>
</body>
{% endblock %}
Thanks in advance! :)
the form "action" attribute is wrong... seeing your urls configuration you dont have a /spirit3/add_patient/ url, I think It is /add_patient/
or you could just use a form tag without an "action" it will post to the current page:
<form role="form" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Create Patient" />
</form>
Hope this helps
As pleasedontbelong mentionned, there's indeed no url matching "/spirit3/add_patient/" in your current url config. What you have in tour root urlconf is:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('spirit3.urls')),
]
This means that urls with path starting with "/admin/" are routed to admin.site.urls, and all other are routed to spirit3.urls. Note that this does NOT in any way prefixes urls defined in spirit3.urls with '/spirit3/', so in your case, all of these urls:
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^patientlist', views.patient_list, name='patient_list'),
url(r'^patientdetail/(?P<pk>\d+)/$', views.patient_detail, name='patient_detail'),
url(r'^add_patient/$', views.add_patient, name='add_patient'),
]
will be served directly under the root path "/" - ie, the add_patient view is served by "/add_patient/", not by "/spirit3/add_patient/".
If you want your spirit3 app's urls to be routed under "/spirit3/*", you have to specify this prefix in your root urlconf, ie:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^spirit3/', include('spirit3.urls')),
]
Note that you can use any prefix, it's totally unrelated to your app name.
As a last note: never hardcode urls anywhere, django knows how to reverse an url from it's name (and args / kwargs if any). In a template you do this with the {% url %} templatetag, in code you use django.core.urlresolvers.reverse().
Hello im learning django and i would like to know if its a way to make my two apps work i have my folders like this
mysite/
--/app1 (blog app)
--/app2 (a list of users that signs up with a form)
--/mysite
--db
--manage.py
my app2 has the index.html and the structure.html which i use to inheritate my others html files, so
im trying to use the post_list.html file that i made in my app1, first of all this is how my urls looks something like this
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'info', 'app2.views.grid'),
url(r'', 'app2.views.home'),
url(r'^blog/', 'app1.views.post_list')
)
my app1 views looks like this
from django.shortcuts import render
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.objects.filter(published_date_isnull=False).order_by('published_date')
return render(request, 'app1/post_list.html',{'posts':posts})
then my post_list.html looks like this
{% extends "/app2/templates/app2/structure.html" %}
{% block content %}
{% for post in posts %}
<div class="post">
<div class="date">
{{ post.published_date }}
</div>
<h1>{{ post.title }}</h1>
<p>{{ post.text|linebreaks }}</p>
</div>
{% endfor %}
{% endblock %}
when i enter to my browser and type 127.0.0.1:8000/blog/ it keeps apearing my app2/index.html, what am i missing? do i have to do anything else than adding my views to my url like i did?
aditional info: i added my app2 and my app1 to my settings thoe
I think your urls.py needs to be updated:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^info', 'app2.views.grid'),
url(r'^$', 'app2.views.home'),
url(r'^blog/', 'app1.views.post_list')
)
url(r'', 'app2.views.home') will act like a wild card since python uses regular expressions to match. Take a look at the URL dispatcher documentation for a better understanding of what's going on.
I'm trying to get a form action redirect to another page, however, I'm not too farmiliar with the Django framework. I went through the polls tutorial.
The current urls.py in my project is:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from WebApp import views
from StripCal import views
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^webapp/', include('WebApp.urls', namespace="WebApp")),
url(r'^stripcal/', include('StripCal.urls', namespace="StripCal")),
)
The url.py in my stripcal app is:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from StripCal import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^run', views.detail, name='detail'),
)
When I type in
http://127.0.0.1:8000/stripcal/
http://127.0.0.1:8000/webapp/
It successfully goes to two different apps. However, I'm not too familiar with {% url 'app_name:view_name' %} syntax. It seems like 'app_name:view_name' becomes /app_name/view_name
This is my current view:
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
# Create your views here.
def index(request):
context = {'somethingDownByCelery': "heh"}
return render(request, 'StripCal/index.html', context)
def detail(request):
context = {'somethingDownByCelery': "heh"}
return render(request, 'StripCal/detail.html', context)
My Index.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'StripCal/index.css' %}"/>
<form action="{% url 'stripcal:detail'%}" method="post">
{% csrf_token %}
<p> StripCal Input </p>
<textarea name="StripCal_Input" cols="30" rows="10"> </textarea>
<br> <br>
<input type="submit" value="Submit">
</form>
My Detail.html
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'StripCal/detail.css' %}"/>
Hello Detail!
When I remove action={% url 'stripcal:detail' %} the webpage loads, however, when I put it in, the page does not even load (HTTP 500).
try to change:
StripCal.url.py
urlpatterns = patterns('StripCal.views',
url(r'^$', 'index', name='index'),
url(r'^run','detail', name='detail'),
)