I'm having trouble getting mine configured correctly.
Have sites enabled and site domain for "SITE_ID = 1" (db object 1 domain) set too "mysite.app"
I have these subdomains setup
ROOT_URLCONF = 'mysite.urls'
SUBDOMAIN_URLCONFS = {
None: 'frontend.urls', # no subdomain, e.g. ``example.com``
'www': 'frontend.urls',
'api': 'api.urls',
}
etc/hosts file
127.0.0.1 api.mysite.app
127.0.0.1 www.mysite.app
127.0.0.1 mysite.app
api/urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
]
api/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse('API')
The frontend app urls and views is identical except for returning string "FRONTEND" in the HttpResponse object.
I can tell django-subdomains is working becuase it does go to the "frontend" app when I hit "mysite.app:8000" vs the mysite.urls as seen in the root_url_conf. It displays "FRONTEND"
But no matter what I do I can't get "api.mysite.app:8000" to hit the api urls file to display "API"
Am I missing something? I'm very new to django. Any help is appreciated.
Thanks.
Simply had to restart the dev server. All was configured correctly.
Try http://api.127.0.0.1.xip.io:8000/ with your dev server running at 127.0.0.1:8000. Since 127.0.0.1 isn't really a domain but an IP, it can't have subdomains. And since your hosts file redirects to 127.0.0.1 not 127.0.0.1.xip.io or similar (you don't have to do it for testing either) you won't be connected.
I haven't used the library that you mentioned but from experience with self-written snippets for subdomains, I'd say this is the case.
Related
I'm quite new to Django and coding to be honest!
I'm facing a problem that appeared out of nowhere when I was finishing a website I can't seem to find anything here that helps with my case.
The problem goes with when I try to run the server with python manage.py runserver it gives me this error :
System check identified no issues (0 silenced).
July 27, 2022 - 17:30:51
Django version 4.0.6, using settings 'sitewilza.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[27/Jul/2022 17:30:53] "GET / HTTP/1.1" 404 2573
Not Found: /
[27/Jul/2022 17:30:57] "GET / HTTP/1.1" 404 2573
and the server returns this:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in sitewilza.urls, Django tried these URL patterns, in this order:
admin/
sobremim [name='sobre']
formacao [name='formacao']
contato [name='contato']
The empty path didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
the problem is I did not change anything in both my urls.py, here they are :
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('website.urls')),
]
&
from django.urls import path
from . import views
urlpatterns = [
path('sobremim', views.sobremim, name='sobre'),
path('formacao', views.formacao, name='formacao'),
path('contato', views.contato, name='contato'),
]
note that i was editing my contato.html file using a anchor to set up a Instagram forward link.
I don't know if my questing is well presented or not, but I hope you guys can understand and thank you in advance for your time in helping me.
So see, you have to understand django flow cycle and then you will be able to develop your website properly.
Here is what you doing in your urls.py when you are requesting your django local server is you are calling urls.py of website app so but you are not telling django what to do exactly whem / is called.
This will fix your problem:
from django.urls import path
from . import views
urlpatterns = [
path('', views.sobremim, name='sobre'),
path('formacao', views.formacao, name='formacao'),
path('contato', views.contato, name='contato'),
]
Update your urls.py file of website app and you will get your response.
I'm using Django 3.0, dj-stripe 2.0, and the Stripe CLI. dj-stripe provides native support for Stripe webhooks, and their documentation says to include the following in my django project's main urls.py file to expose the webhook endpoint:
url(r"^stripe/", include("djstripe.urls", namespace="djstripe")),
I have done this, but how do I now leverage the endpoint?
As a separate experiment, I created a payments app, setup the URL conf, and successfully called the view when triggering the non dj-stripe webhook endpoint via the Stripe CLI. But it doesn't use any dj-stripe functionality:
# project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('stripe/', include("djstripe.urls", namespace="djstripe")),
path('payments/', include("payments.urls", namespace="payments")),
]
# payments/urls.py
from django.urls import path
from . import views
app_name="payments"
urlpatterns = [
path("", views.my_handler, name="my-handler"),
]
# payments/views.py
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
#csrf_exempt
def my_handler(request, **kwargs):
print(request.body)
return HttpResponse(status=200)
Running stripe listen --forward-to localhost:8000/payments/ and, in a separate window, stripe trigger product.created returns a 200 response.
But stripe listen --forward-to localhost:8000/stripe/webhook/ and stripe trigger product.created returns a 500.
Thank you in advance for your help.
[UPDATE]
I have not modified the default DJSTRIPE_WEBHOOK_URL or DJSTRIPE_WEBHOOK_VALIDATION settings. In settings.py I have:
STRIPE_LIVE_PUBLIC_KEY = os.environ.get("STRIPE_LIVE_PUBLIC_KEY")
STRIPE_LIVE_SECRET_KEY = os.environ.get("STRIPE_LIVE_SECRET_KEY")
STRIPE_TEST_PUBLIC_KEY = os.environ.get("STRIPE_TEST_PUBLIC_KEY")
STRIPE_TEST_SECRET_KEY = os.environ.get("STRIPE_TEST_SECRET_KEY")
STRIPE_LIVE_MODE = os.environ.get("STRIPE_LIVE_MODE")
DJSTRIPE_WEBHOOK_SECRET = "*************************************"
The test keys are pulled from env/bin/activate:
export STRIPE_LIVE_PUBLIC_KEY="pk_live_****...."
export STRIPE_LIVE_SECRET_KEY="sk_live_****...."
export STRIPE_TEST_PUBLIC_KEY="pk_test_****...."
export STRIPE_TEST_SECRET_KEY="sk_test_****...."
export STRIPE_LIVE_MODE="False"
When I run stripe listen --forward-to localhost:8000/stripe/webhook/ and trigger stripe trigger customer.created I get the following 500 error:
stripe.error.InvalidRequestError: Request req_NTtzsTFS8uVdfL: No such customer; a similar object exists in test mode, but a live mode key was used to make this request.
But I don't understand how my keys could be getting mixed up, because the webhooks work fine when I trigger the same event and listen via the /payments/ endpoint.
Thank you again for your time.
In summary, you need to decorate a handler function using the #webhooks.handler decorator.
You have some examples in the webhooks section of the dj-stripe documentation.
I have an application running on django. But I am getting error code 404 for some urls even though these are defined.
from .views import check_secret_key # a function in views.py
from .swagger_schema import SwaggerSchemaView # a class inheriting APIView
from .kom.kom_writer import kom_status # a function
from django.conf.urls import url
urlpatterns = [
url(r'^docs/api-docs/', SwaggerSchemaView.as_view()),
url(r'^nag/secret-key/', check_secret_key),
url(r'^nag/kom-status/', kom_status),
]
API curl http://localhost:9999/internal/docs/api-docs/ works fine but curl http://localhost:9999/internal/nag/kom-status/ and nag/secret-key fir 404 errror.
Not FoundThe requested resource was not found on this server.
I am not sure what is it that I am missing.
Note: App was recently updated from DJango 1.8 to 1.11 and djangorestframework 3.2.5 to 3.5.3. Before that it was working fine.
For debugging purpose I am just returning success response right now.
from django.http import HttpResponse
def check_secret_key(request):
r = HttpResponse("I am good", content_type='text/plain', status=200)
return r
well, your definition should be something like
urlpatterns = [
url(r'.*docs/api-docs/', SwaggerSchemaView.as_view()),
url(r'.*nag/secret-key/', check_secret_key),
url(r'.*nag/kom-status/', kom_status),
]
also you need to provide /internal url definition.
I found the issue. Application is using two wsgi.py for running django apps.
Both wsgi.py files were using
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "it.settings.prod")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nt.settings.prod")
Changing it to
os.environ["DJANGO_SETTINGS_MODULE"] = "it.settings.prod"
os.environ["DJANGO_SETTINGS_MODULE"] = "nt.settings.prod"
resolved the problem.
Took reference from https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/modwsgi/
My Django project has two applications in it: a web tool and a REST interface.
I run the REST interface on my database system (e.g. db.myhost.com). This interface only has URL patterns that correspond to the various REST endpoints:
app_name = "rest"
urlpatterns = [
url(r'^report/id/(?P<rid>[0-9]+)/$', views.ReportByID.as_view()),
url(r'^report/slug/(?P<slug>[a-z0-9-]+)/$', views.ReportBySlug.as_view()),
]
Part of the data that these views ultimately show need links to the other application in my project (which I host on a separate system). That application also has URL patterns:
app_name = "mytool"
urlpatterns = [
url(r'^some/cool/path/$', views.some_cool_path),
]
The REST interface only enables the REST URL patterns, since I only want to serve REST endpoints via that host:
# On my REST system
ROOT_URL = "myproject.rest_urls"
Is there a way that I can get the REST application to load the mytool URL patterns without activating them? I don't want a user to be able to browse to db.myhost.com/some/cool/path/ and get an error because that path isn't served on that host, it's served by the web tool server instead. It would be helpful, however, to be able to use reverse() to get the mytool URLs, even if they are just relative fragments (i.e. /some/cool/path ... I could always prepend the server name, which is unlikely to ever change).
I could hard-code the necessary paths, but I'd like to avoid having to do that in case they need to change in the future.
We can do it using django test utils override_settings decorator. It will use temporary settings so, it will not have any effect on the live site.
settings.py
INSTALLED_APPS = [
# .....
'mytool',
# .....
]
ROOT_URL = "myproject.rest_urls"
mytool/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('news/<slug:slug>/', views.NewsDetailView.as_view(), name='news_detail'),
]
mytool/utils.py
from django.test.utils import override_settings
from django.urls import reverse
def temp_reverse(url_conf, url_name, url_args=(), url_kwargs={}):
#override_settings(ROOT_URLCONF=url_conf)
def get_reverse(url_name, *args, **kwargs):
return reverse(url_name, args=args, kwargs=kwargs)
return get_reverse(url_name, *url_args, **url_kwargs)
accessing the reverse for unregistered urls
from mytool.urils import temp_reverse
url = temp_reverse('mytool.urls', 'news_detail', url_kwargs={'slug': 'django-awesome'})
print(url)
# output: /news/django-awesome/
My django app's urls are setup as follows for my development machine:
urls.py:
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^signin$', signin, name='signin'),
url(r'^signout$', signout, name='signout'),
url(r'^advisor/', include('apps.advisor.urls', namespace='advisor')),
]
This setup works fine while developing the app using Django's builtin server (e.g. http://127.0.0.1:8000). I have to deploy the app to a machine and the Django app is exposed by the following url:
http://ip address/appname/
I'd like to have my urls.py file configured so the regular expression in the url tests to see if the url request starts with "appname" or "" (empty string). This method would mean I don't have to create to urls.py files (dev and prod versions). Thanks for any help!
You do not have to do this at all. When your app is deployed via WSGI, the server should pass on the SCRIPT_NAME parameter which identifies the path it is mounted at. As long as you have consistently used {% url %} and reverse() throughout your app rather than hard-coding links, everything will just work.