I am working on django project(which is at verge of completion) and I have structure like something below:
project name: Mysite
App name: myapp
project urls : 127.0.0.1/myapp/blog etc
my requirement is to exclude my app from website (through out) and adding usernames in profile page like:
127.0.0.1/blog
127.0.0.1/products
127.0.0.1/search
and for profile page:
127.0.0.1/simer123/myprofile
I have took from various SO questions and able to exclude the "myapp" part from my urls.
and Also able to include "username" in url for specific page.
SO question1 SO question2 these questions really helped and I found a way out to include user name in my url.
But now again I am stuck because throughout the project I have used things like:
return HttpResponseRedirect(reverse_lazy('myapp:myprofile'))
And similarly in template part like:
<a href="{% url 'myapp:myprofile' %}">
How can I manage to convert them?
Can Anyone explain this with some example.
Thanks
Update:
urls.py file in mysite folder looks like below:
urlpatterns = [
url(r'^autocomplete/', include('autocomplete_light.urls')),
url(r'^$', 'myapp.views.index'),
url(r'^', include('myapp.urls', namespace="myapp")),
url(r'^myapp/', include('myapp.urls', namespace="myapp")),
url(r'^admin/login', adminLogin),
#url(r'^static/(?P<path>.*)$', views.serve),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
url(r'^(?P<username>\w+)/', include('myapp.urls', namespace="myapp")),
url(r'^(?P<username>\w+)/myapp/', include('myapp.urls', namespace="myapp")),
]
urls.py in myapp folder includes urls like below:
urlpatterns = [
# ex: /myapp/
url(r'^autocomplete/', include('autocomplete_light.urls')),
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='user_about'),
url(r'^login/$', views.login, name='login'),
url(r'^newaccount/(?P<uid>.*)/$', views.newaccount, name='newaccount'),
url(r'^logout/$', views.logout, name='logout'),
url(r'^myprofile/$', views.profile, name='profile'),
]
Your base url doesn't need to mention myapp at all, just set your url that includes myapp to r'^'. You can still namespace the urls and everything the same as before.
You can pass a kwargs dict to your reverse and reverse_lazy function in order to mapping it with your url pattern.
reverse_lazy('myapp:profile', kwargs={'username': username})
And in your template, just pass it as positional argument
<a href="{% url 'myapp:profile' username %}">
Related
i want to try another apps in django but i got problem when access another apps.
Page not found
tree:
main:
search:
index.html
scrape.html
preprocessing:
index.html
the scenario like this. from scrape.html i want to access index.html in preprocessing but got an error path not found. I've done to add apps in settings.py
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index,),
path('scrape/',views.scrape,),
]
slice of scrape.html:
Preprocessing
preprocessing url.py
path('', views.index),
let me know where did I go wrong, thx for your help
Your anchor tag is written as:
<a href = "/preprocessing" ...>
The problem here is that your url pattern is like 'preprocessing/', notice that it ends in a trailing slash while your anchors url doesn't. Furthermore in your settings you set APPEND_SLASH = False.
Normally when Django finds a url not ending in a trailing slash it appends a slash to it and redirects the user to this new url. But you have stopped this behaviour by setting APPEND_SLASH = False. As the first step I would advice you to change this back to APPEND_SLASH = True.
Next you should always name your urls and use those names to refer to the urls. So your urlpatterns should be:
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index, name='index'),
path('scrape/',views.scrape, name='scrape'),
]
preprocessing url.py
path('', views.index, name='preprocessing'),
Now in your templates you would simply use the url template tag:
Preprocessing
I'm currently doing on Django project and using reversing the mapper on urls.py. Before I use reversing it worked well, but after I change it to reversing, it started to do not work anymore.
I want to know why it doesn't work. Is it because I didn't add it in proejct file's urls.py? How can I call that url with the name in app's file?
from django.urls import path
from . import views
app_name = 'posting'
urlpatterns = [
path('', views.index, name='index'),
path('<int:post_id>', views.post, name='post'),
path('posting', views.posting, name='postingform')
]
index.html
<a href='{% url 'postingform' %}'>Upload your beer now!</a>
Since you defined an app_name in your urls.py, yo need to specify the name of the view with the app_name:
<a href='{% url 'posting:postingform' %}'>Upload your beer now!</a>
Im trying to add this url to my app's urlpatterns (i.e. MyProject/MyApp/urls.py):
url(r'^login/$', auth_views.LoginView.as_view(), name='login')
I have this snippet in one of my templates:
Login
Normally, clicking on the link takes you to the login page successfully. However, when I try to add a namespace to my urls (app_name = my_namespace) and change the reverse to
Login
it fails when I click on the link and I get the error
Reverse for 'login' not found. 'login' is not a valid view function or
pattern name.
While all the other urls I reverse work with the namespace, it is just the login reverse that fails. Any idea why?
Edit:
MyProject/MyProject/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('ClubInfo.urls')),
]
MyProject/MyApp/urls.py:
app_name = 'clubinfo'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', auth_views.LoginView.as_view(), name='login'),
]
A snippet of the template:
Home
Login
Register
I can click on Home and Register, not login
Edit 2: auth_views is from this import:
from django.contrib.auth import views as auth_views
I think this may have something to do with why the program is raising an error.
It turns out the problem was in my login.html file which Django renders in its LoginView. I didn't use the namespace in one of my reverses in that file.
in your project urls do this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('ClubInfo.urls', namespace='clubinfo')),
]
now in clubinfo urls:
remove
app_name = 'clubinfo'
run de server again and try it should work that my way of doing
I find that your
app_name='clubinfo'
, but your urlpatterns is
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('**ClubInfo**.urls')),
]
Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
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'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]
I am trying to process a form in django/python using the following code.
home.html:
<form action="{% url 'home:submit' %}" method='post'>
views.py:
def submit(request):
a = request.POST(['initial'])
return render(request, 'home/home.html', {
'error_message': "returned"
})
urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^submit/$', views.submit, name='submit')
]
when I try to run it in a browser I get the error:
NoReverseMatch at /home/ u'home' is not a registered namespace
and another error message indicating a problem with the form.
You should just change you action url in your template:
<form action="{% url 'submit' %} "method='post'>
On the note of url namespaces...
In order to be able to call urls using home namespace you should have in your main urls.py file line something like:
for django 1.x:
url(r'^', include('home.urls', namespace='home')),
for django 2.x and 3.x
path('', include(('home.urls', 'home'), namespace='home'))
In your main project, open url.py first. Then check, there should be app_name declared at first. If it is not, declare it.
For example, my app name is user info which is declared in url.py
app_name = "userinfo"
urlpatterns = [
url(r'home/', views.home, name='home'),
url(r'register/', views.registration, name='register')
]
I also faced the same issue.
it is fixed now by adding
app_name = "<name of your app>"
in app/urls.py
For Django 3.0, if you're handling your urls within the app and using include with path, in your project/urls.py:
urlpatterns = [
path(os.getenv('ADMIN_PATH'), admin.site.urls),
path('', include('my_simple_blog.urls', namespace='my_simple_blog')),
path('account/', include('account.urls', namespace='account')),
]
You need to specify namespace in include.
And then in your app/urls.py:
app_name = 'account'
urlpatterns = [
path('register/', registration_view, name='register'),
path('logout/', logout_view, name='logout'),
path('login/', login_view, name='login'),
]
The app_name must match the namespace you've specified in project/urls.py.
Whenever you're referring to these urls, you need to do it like this:
{% url 'namespace:name' %}
If you're using it with redirect:
return redirect('namespace:name')
For the namespace error,
Make sure you have linked the app's url in the main urls.py file
path('app_name/',include('app_name.urls'))
also in the urls.py of your app,make sure you mention the app's name as
app_name='app_name'
Also make sure you have registered the app's name on your installed apps in settings.py
As azmirfakkri has said if you're using redirect, dont use this {% url 'namespace:name' %} syntax, use return redirect('namespace:name').
Probably 2 things could be a root cause,
in app/urls.py do include as below
app_name = 'required_name'
and in project urls.py also include the app_name
url(r'^required_name/$',views.home,name='required_name'),
Check: register app in settings.py INSTALLED_APPS
tag name must be unique in the urls.py file inside your application package inside the project! it is important for the template tagging to route whats what and where.
now [1] inside the urls.py file you need to declare the variable appName and give it the unique value. for example appName = "myApp"; in your case myHomeApp and [2] also define the urlpatterns list...
urlpatterns = [..., url(r'^submit/$', views.submit, name='submit'), ...];
in the html file just change the url tag to:
<form action="{% url 'myHomeApp:submit' %}" method='post'>
this should sifuce... else just write here and we'll see how to continue on
A common mistake that I always find is when you have some name space in your template,
and in YourApp.url you don't have any name space so if you should use name space add
in YourApp.url something like this
app_name = "blog"
then on your temples make sure you add your name space,
so you will have some thing like this "assumption errors are coming from edit.html"
then on that particular template you will do this
"{% url 'blog:vote' pk=post.pk %}" "{% url 'blog:post_category' category.name %}"
if you happen to be nesting include(s, the namespace compounds, eg. topappname:appname:viewname
Maybe someone will find this suggestion helpful.
Go to your applications urls.py and type this before the urlpatterns:
app_name = 'Your app name'
I got the same error below:
NoReverseMatch at /account/register/ 'account' is not a registered
namespace
So, I set "app_name" with the application name "account" to "account/urls.py" as shown below then the error above is solved:
# "account/urls.py"
from django.urls import path
from . import views
app_name = "account" # Here
urlpatterns = [
path("register/", views.register_request, name="register")
]
Check your urls.py
urlpatterns = [
re_path(r'^submit/expense/$', views.submit_expense, name='submit_expense'),
re_path(r'^submit/income/$', views.submit_income, name='submit_income'),
re_path(r'^register/$', views.register, name='register'),
]
then open template.html
put for example register register in your HTML tag like this:
<a class="navbar-brand" href="{% url 'register' %}">
For anyone who struggled on this error like me: After reading the solutions to this question, I was setting namespace in include function in a wrong urls file. Make sure you are modifying the right urls file. For me it was putting it in the main url.py besides settings.py. I hope this answer helps anyone who was confused as I was.
This worked for me:
In urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.index),
path("test/", views.test, name = 'test')]
In views.py:
def test(request):
return render(request, "base/test.html")
In the template:
href="{% url 'test' %}"