ngrok:在 Django 中访问子目录 URL

Posted

技术标签:

【中文标题】ngrok:在 Django 中访问子目录 URL【英文标题】:ngrok: Access subdirectory URLs in Django 【发布时间】:2021-08-29 04:53:55 【问题描述】:

我正在使用 ngrok 来测试我的网站。我可以启动主页,但如何启动除主页以外的目录?比如 127.0.0.1:8000\apartment_listing?我得到Error 500: Internal Server Error。有什么解决办法吗?

模板:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title> Bette | Apartment Listing</title>
</head>
<body>


<head>
% extends 'base.html' %

% load humanize %  
% load leaflet_tags %
    % load geojson_tags %
    % block extra_assets %
        % leaflet_js plugins="forms" %
        % leaflet_css plugins="forms" %
    % endblock %
    % load static %
    % load social_share %

% post_to_facebook http://5442111b8deb.ngrok.io/apartment_listing "Post to Facebook!" %
% post_to_gplus http://5442111b8deb.ngrok.io/apartment_listing "Post to Google+!" %
% post_to_linkedin http://5442111b8deb.ngrok.io/apartment_listing %
% post_to_telegram "New Apartment: object.title" http://5442111b8deb.ngrok.io/ %

<style type="text/css">
.input-group
    margin-bottom:10px;


.card 
  background-color: dodgerblue;
  color: white;
  padding: 1rem;
  height: 4rem;

.cards 
  max-width: 1200px;
  margin-left: 1em;
  display: grid;
  grid-gap: 1rem;


</style>
</head>

<body>

% block map %


 <div class="cards">
          <div class="modal" id="myModal" tabindex="-1">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Apartments Found</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p>
   Nearby Apartments | በቅርብ ያሉ (በ 2km): (apartments|length) 
    % for obj in filter.qs %
         obj.apt_id  - በብር  obj.apt_cost  :  obj.apt_subcity  ; D፡ obj.distance.m|floatformat:"2"|intcomma:False m
        <br />
    % endfor %
</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        
      </div>
    </div>
  </div>
</div>
        </div>
        <div class="card">% include 'django_filter.html' %</div>
         <div class="card">
% leaflet_map "map" callback="ourfunction" %
      
<script type="text/javascript">

var csrftoken = " csrf_token ";

      function csrfSafeMethod(method) 
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
      

      $.ajaxSetup(
        beforeSend: function(xhr, settings) 
          if (!csrfSafeMethod(settings.type) && !this.crossDomain) 
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
          
        
      );

    
var collection =  object_list|geojsonfeature:"popupContent"|safe ;
  console.log(collection);


var myModal = new bootstrap.Modal(document.getElementById("myModal"));
myModal.show();

  

function ourfunction(map, options) 
map.locate(setView: true, maxZoom: 14);

map.on('locationfound', onLocationFound);


    function onLocationFound(e) 


    var radius = e.accuracy;
    var position= e.latlng;
    var counter = 0;
    
    postdate = new Date(); 
    var datetime = postdate.getDate() + "/"
                + (postdate.getMonth()+1)  + "/" 
                + postdate.getFullYear() + " @ "  
                + postdate.getHours() + ":"  
                + postdate.getMinutes() + ":" 
                + postdate.getSeconds();
    var lat= e.latlng.lat;
    var lng= e.latlng.lng;
    
    map.panTo(position,16);
    var user_loc ="Point ("+ lat+" "+lng+")";
    console.log(user_loc);
    console.log(postdate);
    
         
    var form = $('<form action="% url "rent_app:apartment-listing" %" method="POST"> form.as_p % csrf_token % </form>');

    $('body').append(form);

   

   

    L.circle(e.latlng, radius).addTo(map);
L.marker(e.latlng).addTo(map).bindPopup("You are within " + radius + " meters from this point",autoClose:false).openPopup();


function onEachFeature(feature, layer)  

    // does this feature have a property named popupContent?
    if (feature.properties && feature.properties.popupContent) 
        
        layer.addTo(map).bindPopup(feature.properties.popupContent,autoClose:false).openPopup();


;  





    
    //unique autoincrement id for each user location

    function onLocationError(e) 
        alert(e.message);
    

    map.on('locationerror', onLocationError);
        
    L.geoJson(collection, onEachFeature: onEachFeature).addTo(map);
    


      

</script>
  

% endblock %
</div>
</div>

</body>
</html>


C:\Users\Kaleab\Desktop\WebMap\rentapartments>py -3.9 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified some issues:

June 12, 2021 - 18:58:52
Django version 3.2.4, using settings 'rentapartments.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[12/Jun/2021 19:01:19] "GET / HTTP/1.1" 200 4876
[12/Jun/2021 19:01:22] "GET /static/css/bootstrap.css HTTP/1.1" 404 1918
[12/Jun/2021 19:01:43] "GET /static/leaflet/leaflet.css HTTP/1.1" 200 14471
[12/Jun/2021 19:01:43] "GET /static/leaflet/leaflet.js HTTP/1.1" 200 142673
[12/Jun/2021 19:01:43] "GET /static/leaflet/leaflet.extras.js HTTP/1.1" 200 8468
[12/Jun/2021 19:01:46] "GET /static/js/bootstrap.js HTTP/1.1" 404 1912
[12/Jun/2021 19:01:47] "GET /static/js/jquery.bootstrap.modal.forms.js HTTP/1.1" 200 7154
[12/Jun/2021 19:01:48] "GET /static/js/jquery.bootstrap.modal.forms.min.js HTTP/1.1" 200 2608
[12/Jun/2021 19:01:50] "GET /static/js/bootstrap.js HTTP/1.1" 404 1912
[12/Jun/2021 19:01:52] "GET /static/leaflet/images/reset-view.png HTTP/1.1" 200 159
[12/Jun/2021 19:02:01] "GET /apartment_listing HTTP/1.1" 301 0
Internal Server Error: /apartment_listing/
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Python39\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Python39\lib\site-packages\django\template\response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "C:\Python39\lib\site-packages\django\template\response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "C:\Python39\lib\site-packages\django\template\loader.py", line 42, in select_template
    return engine.get_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Python39\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
    return Template(
  File "C:\Python39\lib\site-packages\django\template\base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
    return parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\loader_tags.py", line 278, in do_extends
    nodelist = parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\library.py", line 152, in compile_func
    args, kwargs = parse_bits(
  File "C:\Python39\lib\site-packages\django\template\library.py", line 291, in parse_bits
    args.append(parser.compile_filter(bit))
  File "C:\Python39\lib\site-packages\django\template\base.py", line 563, in compile_filter
    return FilterExpression(token, self)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 662, in __init__
    raise TemplateSyntaxError("Could not parse the remainder: '%s' "
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '://127.0.0.1:8000\apartment_listing' from 'http://127.0.0.1:8000\apartment_listing'
[12/Jun/2021 19:02:03] "GET /apartment_listing/ HTTP/1.1" 500 181555
[12/Jun/2021 19:02:03,305] - Broken pipe from ('127.0.0.1', 56090)

[12/Jun/2021 19:07:58] "GET /apartment_listing HTTP/1.1" 301 0
Internal Server Error: /apartment_listing/
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Python39\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Python39\lib\site-packages\django\template\response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "C:\Python39\lib\site-packages\django\template\response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "C:\Python39\lib\site-packages\django\template\loader.py", line 42, in select_template
    return engine.get_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Python39\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
    return Template(
  File "C:\Python39\lib\site-packages\django\template\base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
    return parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\loader_tags.py", line 278, in do_extends
    nodelist = parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\library.py", line 152, in compile_func
    args, kwargs = parse_bits(
  File "C:\Python39\lib\site-packages\django\template\library.py", line 291, in parse_bits
    args.append(parser.compile_filter(bit))
  File "C:\Python39\lib\site-packages\django\template\base.py", line 563, in compile_filter
    return FilterExpression(token, self)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 662, in __init__
    raise TemplateSyntaxError("Could not parse the remainder: '%s' "
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '://127.0.0.1:8000\apartment_listing' from 'http://127.0.0.1:8000\apartment_listing'
[12/Jun/2021 19:08:00] "GET /apartment_listing/ HTTP/1.1" 500 180768
Not Found: /favicon.ico
[12/Jun/2021 19:10:03] "GET /favicon.ico HTTP/1.1" 404 3968
Internal Server Error: /apartment_listing/
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Python39\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Python39\lib\site-packages\django\template\response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "C:\Python39\lib\site-packages\django\template\response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "C:\Python39\lib\site-packages\django\template\loader.py", line 42, in select_template
    return engine.get_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Python39\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
    return Template(
  File "C:\Python39\lib\site-packages\django\template\base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
    return parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\loader_tags.py", line 278, in do_extends
    nodelist = parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\library.py", line 152, in compile_func
    args, kwargs = parse_bits(
  File "C:\Python39\lib\site-packages\django\template\library.py", line 291, in parse_bits
    args.append(parser.compile_filter(bit))
  File "C:\Python39\lib\site-packages\django\template\base.py", line 563, in compile_filter
    return FilterExpression(token, self)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 662, in __init__
    raise TemplateSyntaxError("Could not parse the remainder: '%s' "
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '://127.0.0.1:8000\apartment_listing' from 'http://127.0.0.1:8000\apartment_listing'
[12/Jun/2021 19:15:43] "GET /apartment_listing/ HTTP/1.1" 500 180768
Internal Server Error: /apartment_listing/
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Python39\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Python39\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Python39\lib\site-packages\django\template\response.py", line 81, in rendered_content
    template = self.resolve_template(self.template_name)
  File "C:\Python39\lib\site-packages\django\template\response.py", line 63, in resolve_template
    return select_template(template, using=self.using)
  File "C:\Python39\lib\site-packages\django\template\loader.py", line 42, in select_template
    return engine.get_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Python39\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Python39\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
    return Template(
  File "C:\Python39\lib\site-packages\django\template\base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 193, in compile_nodelist
    return parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\loader_tags.py", line 278, in do_extends
    nodelist = parser.parse()
  File "C:\Python39\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\Python39\lib\site-packages\django\template\library.py", line 152, in compile_func
    args, kwargs = parse_bits(
  File "C:\Python39\lib\site-packages\django\template\library.py", line 291, in parse_bits
    args.append(parser.compile_filter(bit))
  File "C:\Python39\lib\site-packages\django\template\base.py", line 563, in compile_filter
    return FilterExpression(token, self)
  File "C:\Python39\lib\site-packages\django\template\base.py", line 662, in __init__
    raise TemplateSyntaxError("Could not parse the remainder: '%s' "
django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '://5442111b8deb.ngrok.io/apartment_listing' from 'http://5442111b8deb.ngrok.io/apartm
ent_listing'
[12/Jun/2021 19:16:57] "GET /apartment_listing/ HTTP/1.1" 500 180916
[12/Jun/2021 19:18:11,361] - Broken pipe from ('127.0.0.1', 11253)

[12/Jun/2021 19:18:11,362] - Broken pipe from ('127.0.0.1', 52355)

【问题讨论】:

开启 DEBUG 并向我们展示输出。 @Ene P 我发布了错误日志。 您的模板有问题。你也可以发一下吗? @EneP 添加了模板。 【参考方案1】:

您需要将这些 URL 用引号括起来,并检查哪个是正确的顺序,URL 在前还是标题在前,因为这些是自定义标签,我不知道如何使用它们。

% post_to_facebook http://5442111b8deb.ngrok.io/apartment_listing "Post to Facebook!" %
% post_to_gplus http://5442111b8deb.ngrok.io/apartment_listing "Post to Google+!" %
% post_to_linkedin http://5442111b8deb.ngrok.io/apartment_listing %
% post_to_telegram "New Apartment: object.title" http://5442111b8deb.ngrok.io/ %

【讨论】:

以上是关于ngrok:在 Django 中访问子目录 URL的主要内容,如果未能解决你的问题,请参考以下文章

在 Shopify 上使用 ngrok 的 Django

使用 xampp 和 ngrok 访问本地虚拟主机时禁止访问

Ruby on Rails:如何通过 ngrok 访问 Action Cable?

Django:Hellosignup 回调 url 不来查看

在 IIS 和 ngrok 上部署 Django

配置django上传文件目录的http访问