Forums

Can't Run My site Locally through the bash console

Hello , im new to django and your site as well, i have succeessfully hosted my site on your server so myusername.pythonanywhere.com , i am having some bug in my code something is not working as expected idk if you guys also aid with code ? but non the less i checked the regular error logs and the likes and server logs and nothing comes up no traceback. i have been trying to run it locally through the bash it generates the link but when i click on it it says " ** Site Cant be reached **"" for some reason ?

i also have an issue with multiple ports being in use and i cant use any generic commands to kill those ports so how do i kill a port so i can re use it ?

below are a list of commands ive tried and have failed. lsof -ti:8000 | xargs kill sudo lsof -i :<port_number> fuser 8000/tcp sudo lsof -i :8000

to keep it more simple and to 1 issue, what are the potential reasons it keeps saying " Site cant be reached " after trying to run " Python Manage.py runserver" when i click on the link. i checked its not a firewall issue any help greatly appreciated

That will not work on PythonAnywhere -- you have to deploy the web app on the Web page, see our help pages for more details.

oh i never knew that, for context i already have my web app up and running on pythonanywhere.com The code is not working as expected some bug is in the code , in the error log nothing comes up so i was hoping by running it locally " manage.py runserver " it might give me more info on whats going on in the console.

i have the app with the exact same code running on Visual studio and it has no bug but when i try it on my pythonanywhere its not working

any reason why that might be ?

How do you run it on PythonAnywhere? How is it not working?

so on pythonanywhere i used the normal setup following the article that says "Deploying an existing Django project on PythonAnywhere"

so there is no issue there

the problem i have is that the code isn't responding as expected and the error logs or server logs seem to indicate there is no issue

i dont know if you want me to paste the code in question here ? or if you guys help people with code in there app its something to do with either my (Views.py / URLS.py / Admin.py) one of those 3

its important to note that the App is identical to the App i ran locally on VIsual studio IDE yet there is no bug or errors that show up on that end

Are the errors in the error log not relevant to your issue or you don't see any errors at all?

they do not display any errors relevant to the issue at hand , if there is any error i fixed them so it says everything is fine but the functions arent behaving as expected

also i was disappointed to see i cant use print statements in functions to see what's going on in them

cant see any output in the logs from my print statements etc

You can see print statements in the logs. stdout in server log and stderr in error log.

ok so i got the server logs working i seem to have narrowed down my issue with print statements for some reason my URL is having issues

does pythonaywhere generate or proccess URL's in a unique way that could be causing these issues ? the same identical code works when run locally but on the site it seems to have a bug and doesnt execute as expected

are we allowed to post snippets of code perhaps ? idk if u guys are willing to help with something like that

Could you explain what do you mean by "my URL" in this context and what is the nature of the issue related to urls? Maybe provide some examples, if possible.

admin.py

def payments(self, obj):

        url = reverse('customer_payments', args=[obj.pk])
        print("Generated URL:", url)
        print("Customer ID:", obj.pk)
        return format_html('<a href="{}">Customer Payments</a>', url, obj.payment_set.count())
    payments.short_description = 'Payments'

view.py

def customer_payments(request, customer_id):

    print("Customer ID received:", customer_id)
    customer = get_object_or_404(Customer, pk=customer_id)
    print("Customer object retrieved:", customer)
    payments = Payment.objects.filter(customer=customer)
    context = {
        'customer': customer,
        'payments': payments,
        'payment_time': datetime.now()
    }
    return render(request, 'customer_payments.html', context)

urls.py

path('customer/<int:customer_id>/payments/', views.customer_payments, name='customer_payments')

when i click on customer payments link in the admin panel it wont execute and gives error

Customer with ID “9/payments” doesn’t exist. Perhaps it was deleted? "

i have already confirmed with print statements its collecting the correct customer id and generating the correct URL i have done migrations i have checked installed apps and middleware section in settings all is well root URL in projects url py file also correctly links to the URLS.py for my app all this has been checked and no issues seen.

[edit by admin: formatting]

That looks like you may have another path that is taking precedence over the one you're trying to access. It would be something like customer/<int:customer_id>/

i have several that are similar to that

they look something like this `

path('customer/<int:customer_id>/create/', views.create_order, name='create_order'),

path('customer/<int:customer_id>/payments/', views.customer_payments, name='customer_payments'),

path('customer/<int:customer_id>/payment/create/', views.create_payment, name='create_payment'),

the only one that looks Exactly like that is the following

path('customer/<int:customer_id>/', views.customer_orders, name='customer_orders')

Django runs through your patterns in order until it finds one that matches the URL (https://docs.djangoproject.com/en/4.2/topics/http/urls/) If you have the shorter patterns after the longer patterns, the long pattern will match before Django gets to the shorter one. Make sure that your shorter patterns are before the longer ones.

i have done that issue still persists also like i mentioned before if this code runs locally on vs code with no issues than it seems to not be about how Django matches URL patterns.

i also have a similar error when trying to get to my customer orders

Admin.py**

 def customer_orders(self, obj):
    url = reverse('customer_orders', args=[obj.pk])


    return format_html('<a href="{}">Customer Orders</a>', url)

customer_orders.short_description = "Order History"

Views.Py

def customer_orders(request, customer_id):
customer = get_object_or_404(Customer, id=customer_id)
orders = Order.objects.filter(customer=customer)
return render(request, 'orders/customer_orders.html', {'customer': customer, 'orders': orders,'create_order_text': 'Create Order'})

Urls.py

path('customer/<int:customer_id>/', views.customer_orders, name='customer_orders')

instead of directing me to the Customer_Orders.html template it directs me to the customer change page showing the customers details like name email address etc again this issue is non existent when run locally

Hey, if you code is running locally as you expect are you sure that the code on PythonAnywhere is the same as you have locally?

yes the project has been cloned i just copied pasted the directories over to pythonanywhere and uploaded them no alteration has been made until after these errors came up , i started adding print statements and the likes

but originally nothing has been changed other then the additional things needed in the settings and things related to deployment the app its self has the same code in the views , models , urls etc

What is the URL in the browser's location bar in that last case, where you're trying to get the customer orders template?